Click to See Complete Forum and Search --> : Problems with the event handler


nightscorpion
April 29th, 2009, 02:16 AM
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);
}

boudino
April 29th, 2009, 02:40 AM
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.

nightscorpion
April 29th, 2009, 04:04 AM
hey..

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

JonnyPoet
April 29th, 2009, 05:51 AM
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

nightscorpion
April 30th, 2009, 02:42 AM
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

boudino
April 30th, 2009, 03:43 AM
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

public void handler1(object sender, EventArgs e)
{
Button b = (Button)sender;
TabPage tp = (TabPage)b.Parent;

mainprogram.AddNewStereotype(tp, tp.Text);
}

JonnyPoet
April 30th, 2009, 03:47 AM
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.

nightscorpion
April 30th, 2009, 04:12 AM
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

JonnyPoet
April 30th, 2009, 05:55 AM
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:D