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.
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

New Topic/Question
Reply




MultiQuote



|