Alex
Alex

Reputation: 11137

determine user-control caller

i created a user-control that would be a button . On my form i have placed multiple such buttons . My question is : How do i determine in my user-control class who called me (i.e. what button ) ?

Upvotes: 0

Views: 136

Answers (1)

vgru
vgru

Reputation: 51224

Your button class should have a public Clicked event, like a normal WinForms button:

 class MyButton
 {
     // this should be fired when a button is clicked
     public event EventHandler Clicked;
 }

If you have a single event handler for multiple buttons, e.g.:

 button1.Clicked += new EventHandler(button_Clicked);
 button2.Clicked += new EventHandler(button_Clicked);
 button3.Clicked += new EventHandler(button_Clicked);

You can check the sender parameter in your handler to see which control fired the event:

 private void button_Clicked(object sender, EventArgs e)
 {
     MyButton button = sender as MyButton;
     MessageBox.Show("You clicked on " + button.Text");
 }

Upvotes: 2

Related Questions