0

Hey everybody,

I have multiple buttons on a form, I need to make them all have the same click event, which displays the name of the sender button.

I knew that in order to make them have the same event handler, I should make there delegates refer to the same function.

but how shall I access the properties of the sender in this function.

2
Contributors
3
Replies
8
Views
8 Years
Discussion Span
Last Post by JerryShaw
0

Basically, it is just a matter of first, make sure the sender is a button type, then cast sender as a button, then you have the originating button at your disposal.

Looking for something like this ?

private void button1_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button btn = (Button)sender;
                MessageBox.Show(btn.Name + " pressed");

                btn.Text = "MeHo";
                if (btn == button2) // something special if it was from button2
                    btn.Text += "-2";
            }
        }

//Jerry

0

Yes, thank you very much jerry, that's what I've been looking for.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.