Programming Windows Forms

Programming, for all ages and all languages.
Post Reply
icealys
Member
Member
Posts: 60
Joined: Mon Feb 17, 2014 3:54 pm

Programming Windows Forms

Post by icealys »

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.
Peterbjornx
Member
Member
Posts: 116
Joined: Thu May 06, 2010 4:34 am
Libera.chat IRC: peterbjornx
Location: Leiden, The Netherlands
Contact:

Re: programming windows forms

Post by Peterbjornx »

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
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Programming Windows Forms

Post by SpyderTL »

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
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Programming Windows Forms

Post by SpyderTL »

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
kutkloon7
Member
Member
Posts: 98
Joined: Fri Jan 04, 2013 6:56 pm

Re: Programming Windows Forms

Post by kutkloon7 »

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:

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!");
    }
}
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Programming Windows Forms

Post by iansjack »

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.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Programming Windows Forms

Post by AndrewAPrice »

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.
My OS is Perception.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Programming Windows Forms

Post by jnc100 »

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