Event for Click in any button (C# windows forms)

前端 未结 4 1404
广开言路
广开言路 2020-12-21 03:35

I\'m developing a program that has many buttons that should do a similar action when clicked, but with a small difference based on which button was clicked. The problem is t

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-21 04:06

    Is there a way to program simply one block that would get the click on any button and which button was clicked?

    I would just use the same click event and conditionally check the sender for which button was clicked

    private void button1_Click(object sender, System.EventArgs e)
    {
       if(sender == button1)
       {
          //do something different for button1
       }
       else if(sender == button2)
       {
          ....
       }
    }
    

    Or a switch statement..

提交回复
热议问题