Programming Windows Forms
Programming Windows Forms
I came across this statement and i'm not sure what it does. this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);. What does Click = Click + EventHandler even do? That statement makes no sense to me. 1st- Click isn't even assigned to anything yet . 2nd- is EventHandler a constructor function? I'm confused.
-
- Member
- Posts: 116
- Joined: Thu May 06, 2010 4:34 am
- Libera.chat IRC: peterbjornx
- Location: Leiden, The Netherlands
- Contact:
Re: programming windows forms
Im not that familiar with .NET nor do i want to be but it appears as if the += operator is overridden to provide a list_add functionality, you are adding the handler to a list of handlers rather than setting a singular handler callback to your handler
Re: Programming Windows Forms
I'm more of a C# guy, but I believe that this statement will add a new event handler to an event handler collection. I believe that it will also automatically create the event handler collection if it is currently null. Sort of a "safe" collection add method.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Re: Programming Windows Forms
System is a namespace. EventHandler is a class. I assume that gcnew is a keyword (or macro) that will call the appropriate constructor and return the instance that is created.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Re: Programming Windows Forms
What Peterbjornx says: it adds an event handler to the click event, so that the event handler gets called if the button is clicked.
edit: This little program might clearify:
edit: This little program might clearify:
Code: Select all
using System;
using System.Windows.Forms;
class Program
{
static void Main(string[] argv)
{
Form f = new Form();
Button b1 = new Button() { Text = "Add EH" }, b2 = new Button() { Text = "Remove EH", Top = 40 }, b3 = new Button() { Text = "Go", Top = 80 }; ;
b1.Click += (Object o, EventArgs e) => { b3.Click += clickHandler; };
b2.Click += (Object o, EventArgs e) => { b3.Click -= clickHandler; };
f.Controls.Add(b1);
f.Controls.Add(b2);
f.Controls.Add(b3);
Application.Run(f);
}
static void clickHandler(object sender, EventArgs e)
{
MessageBox.Show("Eventhandler is executed!");
}
}
Re: Programming Windows Forms
I think it is incorrect to suppose that there is nothing assigned to the Click event handler initially. Even if the programmer adds nothing there is a default action associated with clicking a button therefore, I presume, there is a default event handler.
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: Programming Windows Forms
There may be something assigned to it initially (the code that causes it to draw pushed down), or that may be handled in the code right before or after calling the event.
From my understanding an event handler is a wrapper around a delegate. The only special thing about it is that it's a linked list or something that allows you to insert, delete, or iterate over the delegates assigned to an event.
From my understanding an event handler is a wrapper around a delegate. The only special thing about it is that it's a linked list or something that allows you to insert, delete, or iterate over the delegates assigned to an event.
My OS is Perception.
Re: Programming Windows Forms
Defining the Click event in the Control class causes the compiler to insert code to add an entry to the non-public 'Events' field for the class, along with adding code to override the add/remove operators on the Click event to manipulate this.
The initial contents of Events[Control.EventClick] is empty. This can be verified by inspecting it with reflection (see, e.g. here).
The code to change the appearance of the control following a mouse action is either in the OnClick method of the Button/ButtonBase (which does some internal state processing then calls any registered handlers of the Click event) or more likely within the Win32 API that backs up Windows Forms. In the case of Button I think its in the Win32 code as overriding OnClick with an empty function does nothing to the appearance changes.
Regards,
John.
The initial contents of Events[Control.EventClick] is empty. This can be verified by inspecting it with reflection (see, e.g. here).
The code to change the appearance of the control following a mouse action is either in the OnClick method of the Button/ButtonBase (which does some internal state processing then calls any registered handlers of the Click event) or more likely within the Win32 API that backs up Windows Forms. In the case of Button I think its in the Win32 code as overriding OnClick with an empty function does nothing to the appearance changes.
Regards,
John.