4 Replies - 4130 Views - Last Post: 02 November 2010 - 03:11 PM Rate Topic: -----

#1 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Where is the event, only delegates in button.click

Posted 02 November 2010 - 09:35 AM

Hi!

A form with two checkboxes and two buttons. The button click functions like this:
        private void button1_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = !checkBox1.Checked;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            checkBox2.Checked = !checkBox2.Checked;
        }



In the Form1.Designer.cs the following (notice the last row):
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(205, 209);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(205, 238);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 3;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);



Right-click EventHandler and choose Go to definition gives this:
using System.Runtime.InteropServices;

namespace System
{
    // Summary:
    //     Represents the method that will handle an event that has no event data.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     An System.EventArgs that contains no event data.
    [Serializable]
    [ComVisible(true)]
    public delegate void EventHandler(object sender, EventArgs e);
}



Now right-click "Click" in the Form1.Designer.cs and choose Go to definition and expand the EventHandler Click which gives this:
        //
        // Summary:
        //     Occurs when the control is clicked.
        public event EventHandler Click;



And right-click "EventHandler", choose Go to definition which gives this:
using System.Runtime.InteropServices;

namespace System
{
    // Summary:
    //     Represents the method that will handle an event that has no event data.
    //
    // Parameters:
    //   sender:
    //     The source of the event.
    //
    //   e:
    //     An System.EventArgs that contains no event data.
    [Serializable]
    [ComVisible(true)]
    public delegate void EventHandler(object sender, EventArgs e);
}


And this is the same as above...

So, What is happening here... The row
  this.button2.Click += new System.EventHandler(this.button2_Click);


"translates to" EventHandler1 = EventHandler1 + EventHandler2; which I interpret as delegates being added to a delegate since EventHandler is a delegate.

Does this mean that EventHandler1 is a multi cast delegate?

Where is the event?
I mean; here we are doing stuff with delegates but where is the code that tells the delegate to listen to the button click event? I have an idea that this row...
public event EventHandler Click;


...is responsible for that. Would you care to explain this row to me? It looks a little odd to me since we are assigning the "Click" variable both the type delegate and the type event... :/

And where is the code that actually creates the event? Or, if that is a weird question, how is an event in general created from code?

I'm trying to get my head around delegates mainly but they get tangled with events all the time. :) And this leads to above questions.

Thanks
Jens

PS: Edited in vain to try to get C# highlighting, how do I do that? [ code=csharp] (without the space) will not do the trick.

This post has been edited by jens: 02 November 2010 - 09:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Where is the event, only delegates in button.click

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Where is the event, only delegates in button.click

Posted 02 November 2010 - 09:47 AM

this.button2.Click += new System.EventHandler(this.button2_Click);


Basically, this line shows that you are subscribing the function button2_Click() to the Click event of the button2 control. What this means is when button2 gets clicked, it'll fire that event, and execute all subscribing functions. That's why it's done as +=, as you're wanting to add that function as a subscribing event handler for that event. Naturally, to unsubscribe, you'd use -= instead.

I hope this helps it make a little more sense.
Was This Post Helpful? 0
  • +
  • -

#3 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Where is the event, only delegates in button.click

Posted 02 November 2010 - 11:01 AM

Well, thanks for your time but I guess I didn't explain what I wanted...

I do understand that "if I do this" "that will happen" - thing is I want to understand what's going on behind what meets the eye. As demonstrated above the row in question actually (if I understand this correctly) only adds a delegate to a delegate. Where is the delegate coupled to the event? And where is the code firing the event? What is an event? Is it like an interrupt in a processor? Or is it just a method call that (must/can/usually) leads to a delegate?

Why do we use delegates at all? I could have a method A with a list of methods to call when method A is called instead of a multicast delegate. I could have a method in a class that implements an interface instead of a delegate.

I just don't get it. And on top of this the darn events. :)

Sorry for ranting.
/Jens
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Where is the event, only delegates in button.click

Posted 02 November 2010 - 12:25 PM

Try these two links.

http://msdn.microsof...y/17sde2xt.aspx
http://blog.monstuff...ves/000040.html

The second one is kinda old, but I believe goes through the same thinking that you are. Maybe it will help out.
Was This Post Helpful? 1
  • +
  • -

#5 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Where is the event, only delegates in button.click

Posted 02 November 2010 - 03:11 PM

Thanks!

And for anyone reading this and wondering I found http://www.codeproje...ndamentals.aspx which is helping me greatly at the moment.

Regards
Jens
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1