简体   繁体   中英

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 (ie what button ) ?

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, eg:

 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");
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM