Dec 24, 2015

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

Assign the same event handler to all buttons.
foreach (var button in Controls.OfType<Button>()) {
    button.Click += button_Click;
}
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.

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib