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.

Recommended Answers

All 3 Replies

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

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

Please mark as Solved
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.