Home > Software design >  How to print the name of the button which is clicked in C#?
How to print the name of the button which is clicked in C#?

Time:08-05

I am adding dynamic buttons to the form with button click. And the whole buttons has same names. Also i add toolstripmenu to all buttons. I want to do is when i press to the DeleteCity from ToolStripMenu, my button must be remove from my form. I tried to remove it like this:

this.Controls.Remove(-button_name-);

But i dont know the name of the button.How to i reach the clicked button name? Thanks for your help.

CodePudding user response:

In your eventhandler of the button_clicked event you get the sender of the click event (e.g. the button). You can get all the buttons information including the name from this.

It could look something like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    var test = button.Name;
}
  • Related