views:

94

answers:

2

Hi to all,

At my program i dynamicly add Buttons to my form

{
   ...
   Button bt = new Button();
   bt.Text = "bla bla";
   bt.MouseClick += new MouseEventHandler(bt_MouseClick);
   myPanel.Controls.Add(bt);
   ... 
}

void bt_MouseClick(object sender, MouseEventArgs e)
{
    TabPage _tab = new TabPage();
    _tab.Text =  ??? // I want to get the Button's text ! this.Text returns me the
                     //main form.Text 
}

How can access my dynamic Buttons properties ? How can I understand whick button is clicked either getting its text.

Thanks.

+2  A: 

Hi,

void bt_MouseClick(object sender, MouseEventArgs e)
{
    TabPage _tab = new TabPage();
    _tab.Text =  ((Button)sender).Text;
}
IordanTanev
+1  A: 

When an EventHandler delegate is invoked, the sender parameter is the component that raised the event, and the e parameter is a subclass of EventArgs that provides any additional component/event specific information for the event.

Therefore you can establish which button the event fired on by casting the sender parameter to Button:

void bt_MouseClick(object sender, MouseEventArgs e)
{
    var button = (Button)sender;
    TabPage _tab = new TabPage();
    _tab.Text =  button.Text;
}
Neil Barnwell
thanks for the answer
Cmptrb
Erm, thanks. Maybe you could consider up-voting or an accepting one of the answers?
Neil Barnwell
i did it, thanks again.
Cmptrb