CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Problems with the event handler

  1. #1
    Join Date
    Apr 2009
    Posts
    18

    Problems with the event handler

    Hello everyone,
    i am having problems in passing arguments to the event handler which i am trying from the past 2 days and not able to solve it.

    my code looks like this below. I try to Create a button for the tabpages dynamically.Lets say we have 4 tabpages and i have created 4 buttons for each of these tabpages. When i click on the button of the 2nd tab page i would like to send the name of the this tabpage to my event handler in order to send it as an argument for the method(addform) that i call in the event handler..however tp.text takes always the last tabpage name in the event handler and not the 2nd tabpage.

    would be gratefull for any help


    thank u
    public bool CreateButton(TabPage tp, Object parentobj)
    {
    Button button = new Button();
    button.Location = new System.Drawing.Point(250,440);


    tp.Controls.Add(button);
    parentglobalobj = parentobj;
    button.Click += new EventHandler(handler1);
    return true;
    }

    public void handler1(object sender, EventArgs e)
    {
    // tp.text takes the last tabpage name n not the tabpage from where the respective button was created
    test.addform(parentglobalobj, tp.text);
    }
    Last edited by nightscorpion; April 29th, 2009 at 04:03 AM.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Problems with the event handler

    Even I don't know what "parentglobalobj", but I think it is key to your problem. You set it every time CreateButton() is called and it holds value set in last call. In handler1(), you use this value, so you every time you pass the same value to test.addform(), which is last one set in CreateButton.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Apr 2009
    Posts
    18

    Re: Problems with the event handler

    hey..

    i edited the code ....i guess u misunderstood me..now it should be clear from the code itself

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Problems with the event handler

    Quote Originally Posted by nightscorpion View Post
    Code:
    public bool CreateButton(TabPage tp, Object parentobj)
    {
        Button button = new Button();
        button.Location = new System.Drawing.Point(250,440);
        tp.Controls.Add(button);
        parentglobalobj = parentobj;
        button.Click += new EventHandler(handler1);
        return true;
    }
     
    public void handler1(object sender, EventArgs e)
    {
        / / tp.text takes the last tabpage name n not the tabpage from where the respective button was created
        test.addform(parentglobalobj, tp.text);
    }
    Hi !
    At first please use codetags as it is written in our forum rules
    Its needed because code looses formating when nor done between Codetags. The advanced editor has a button for it to create them.
    Second problem is:
    I cannot see in your code the scope of your variables
    So what is the scope of your parentglobalobj whats the scope of test and how this is defined also whats tp defined in handler1
    if it is the TabPage wherefrom did you get this tp ?
    I'm asking because this may be your problem. If tp is global sope in this Form and you have created one TabPage and used tp to keep its adress then this is overwritten when you are creating the next tab page so you always will get the last created page.

    I cannot find out what you are trying to accomplish
    Why not simple using the tab buttons itself ? The TabControl has a property where you will be able to access every single Tabpage.
    Simple use a TabControl and add all your pages to it. In the TabPages[i] you will find them again.

    Additional: Using Objects ( if not absolutly necessary) is a bad habit and should not be used whenever possible. Use generic Lists when necessary or for a parent you will normally know what Type of class the parent is. Interfaces could also be used, Objects - no ! Its quite the same like driving without security belt.

    Next Question: You are creating the button in a method which returns a boolean. The code within that method has no chance not to return true. Yes per sure it can maybe create an exception before it is ready and then the result would not be true. But there is no try /catch for an exception in this method, so if that happend ( I personally cannot imagine why this should happen ) the code will crash anyway. So this method could simple be void instead of bool
    Last edited by JonnyPoet; April 29th, 2009 at 05:58 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Apr 2009
    Posts
    18

    Re: Problems with the event handler

    Code:
     public partial class Form1 : Form
        {
    
           Object parentglobalobj;
            string globalTabname;
           CSAddinDemo.Main mainprogram = new CSAddinDemo.Main();
    
         public void CreateLabel(TabPage tp, int i, string labeltext.....
        {
        }
    
      public TabPage CreateTab(string tabname)
            {
                TabPage tabpage = new TabPage(tabname);
                tabControl1.TabPages.Add(tabpage);
                return tabpage;
    
            }
    
         public void CreateButton(TabPage tp, Object parentobj)
            {
                Button button = new Button();
                button.Location = new System.Drawing.Point(250,440);
                globalTabname= tp.Text;  //not sure if this part is needed 
                tp.Controls.Add(button);
                parentglobalobj = parentobj;
                button.Click += new EventHandler(handler1);
     
                
            }
    
            public void handler1(object sender, EventArgs e)
            {
    
    // globaltabname tell me actually the kind of the ele/package itself. but this globaltabname returns always the name of the last tab page and not the name of the tabpage from where the button was clicked
    
               mainprogram.AddNewStereotype(parentglobalobj, globalTabname);
    
            }
    
    }
    
    ----------------
      public class Main
        {
    ....
    ....
    .....
    
       public  void AddNewStereotype(Object obj,string tabname){
            EA.Package package;
              EA.Element element;
    
            if ((package = obj as EA.Package) != null)
            {
            
              
                object newPackage = package.Packages.AddNew("testbArea3", ""); 
    
                
                EA.Package elem = (Package)newPackage;
              
                
               elem.Update();
          
        
              // elem.Element.Stereotype = "ProcessArea";
               elem.Update();
            }
            else if ((element = obj as EA.Element) != null)
            {
    // not yet tried for any element yet.
            }
    
    
        }
        }
    i have submitted a part of my code. my main aim was to read an xml file and create a windows form. i have tags in my xml file which are meant to create tab pages (each of the tab pages can contain either elements or packages but not both ). For each of these tab pages i am supposed to create a button. when the user clicks on the button it should create a package/element respectively . The tabpage name itself tells me if it contains an element or a package. Thats the reason y i need the name of the tabpage so that i know wht kind of an object i need to add.

    Secondly the argument in the CreateButton which sends the parentObj is nothing but the name of the xmlfile. The xmlfile is actually the representation of a package/element.

    so u can say that this whole thing is like a tree representation. When a click on a parent(ele/Pac) it reads the xml file and creates a winform. it further contains children which can again be pac/ele. Packages and elements of the same kind are put in one tab. with the help of the ADD button in the respective tab pages i should be able to create that kind of ele/package.

    i hope this wasnt too confusing.




    MY PROBLEM : is i want only the name of the respective tabpage where the button was clicked ...thats it nothing more nothing less


    thank u

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Problems with the event handler

    Read my previous repla, it is still valid. You cannot use parentglobalobj and globalTabname in the way you are using it.

    The solution could be something like
    Code:
    public void handler1(object sender, EventArgs e)
    {
      Button b = (Button)sender;
      TabPage tp = (TabPage)b.Parent;
    
      mainprogram.AddNewStereotype(tp, tp.Text);
    }
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Problems with the event handler

    whats about storing the needed name in the buttons 'Tag' using boxing (as you can store any object there) so you may use this for stoing the needed name there and can easily getting back it from the created tag object by unboxing it again.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Apr 2009
    Posts
    18

    Re: Problems with the event handler

    thank you both for the help.

    i just inserted the code from boudino n it worked perfectly fine..
    thank u both again for the help

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Problems with the event handler

    Quote Originally Posted by nightscorpion View Post
    thank you both for the help.

    i just inserted the code from boudino n it worked perfectly fine..
    thank u both again for the help
    So if boudinos code hasd helped you dont forget to rate his post
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)