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

前端 未结 4 1388
广开言路
广开言路 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:09

    Assign the same event handler to all buttons.

    foreach (var button in Controls.OfType

    Or you can select the same event handler in the properties window switched to events (flash icon).


    private static void button_Click(object sender, EventArgs eventArgs)
    {
        switch (((Button)sender).Name)
        {
            // find a way to disambiguate.
        }
    }
    

    You can also add some useful information to the Tag property for the disambiguation. And last but not least, you can derive your own button from Button and add appropriate properties. They will even appear in the properties window.

提交回复
热议问题