Hey forum,

When we make a button on our WPF form and double click on it, we get the following code generated for us:

private void button1_Click(object sender, EventArgs e)
        {

        }

Now obviously, the button is a _Click method which basically means that whatever code that goes within the body of this method will be executed when the button is 'Clicked'.

With that said, can anyone be so kind to actually explain the signature of the method:

(object sender, EventArgs e)

It would really help me get an understanding of what that actually means. I've made a bunch of UI's and not been bothered to investigate as to what some of the pre-generated code actually mean.

All help is extremely and gratefully appreciated.

Usmaan.

Recommended Answers

All 2 Replies

I think the 'object sender' is the control that generated the event, it could be a button on your form or the form it's self.

The event args has more information about the event.

Make a button on your form generate an event when clicked and try this:

Button btn = sender as Button;
btn.Text = "Clicked!";

You can get all this information from MSDN, which if you haven't checked out it's probably important that you do so.
http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx
if you scroll down you will see an event named "Click"

Ah okay. I'll do that. And oh, this is my first time in my life that I have seen the 'as' keyword but, I can figure out what your code means. You're correct about msdn holding valuble information but, sometimes I feel almost dislexic when attempting to make sense of the information.

Thanks for the reply though, much appreciated.

Usmaan

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.