How do events in C # trigger?

3

Passing through EVENTS and DELEGATES in C #, I find the Click event in almost all the controls, I want to make a personalized event, I have tried to see the definition of the "CLICK" event. I understand that the next line invokes the delegate and refers to the method that is between parentheses and that method is what should be done when the event is triggered, what I do not understand what is the method or where is the definition that says when the event is triggered. I hope you can help me understand more about the topic and make my personalized event.

this.btnAceptar.Click += new System.EventHandler(this.btnAceptar_Click);
    
asked by Aetos2501 11.07.2018 в 16:37
source

2 answers

3

Your question mixes a couple of things.

On the one hand, it seems that you do not understand at what time and from where the event is triggered. For these cases, it is very useful to know that the source code of C # is open source and is available for consultation.

If we look at the source code of Button , we see the following:

protected override void OnClick(EventArgs e) {
    Form form = FindFormInternal();
    if (form != null) form.DialogResult = dialogResult;


    // accessibility stuff
    //
    AccessibilityNotifyClients(AccessibleEvents.StateChange, -1);
    AccessibilityNotifyClients(AccessibleEvents.NameChange, -1);

    base.OnClick(e);
}

As we can see, OnClick is overwriting the Method OnClick of the base class. If we go to the definition, this is:

public class Button : ButtonBase, IButtonControl 

In ButtonBase we find the following:

protected override void WndProc(ref Message m) {

        switch (m.Msg) {
            // NDPWhidbey 28043 -- we don't respect this because the code below eats BM_SETSTATE.
            // so we just invoke the click.
            //
            case NativeMethods.BM_CLICK:
                if (this is IButtonControl) {
                    ((IButtonControl)this).PerformClick();
                }
                else {
                    OnClick(EventArgs.Empty);
                }
                return;
        }
       ...

That is, the event OnClick is called when the message is received from the windows message system BM_CLICK (I understand that it is a message that is sent when detected at the press of a button).

The base class of ButtonBase is Control :

public abstract class ButtonBase : Control 

If go to it , for end we find the definition of the event and the manager:

private static readonly object EventClick = new object();

...
public event EventHandler Click {
    add {
        Events.AddHandler(EventClick, value);
    }
    remove {
        Events.RemoveHandler(EventClick, value);
    }
}

As you can see, you have not chosen a very simple example if you wanted to see how to generate a personalized event. This is actually very simple in its most basic case. First, the event is defined:

public event EventHandler MiEvento;

Then, when you want to launch it, simply verify that it exists and call it:

if (this.MiEvento!= null)
    this.MiEvento(this, null);   

The parameters are the sender (the class that is launching the event) and the parameters. In this example I do not send parameters, you can send any type of parameters but explain how to define them would make this response longer than it already is.

To receive the event in another class, you simply have to subscribe to it and create the handler that will receive it:

ObjetoDeLaClaseQueDefineElEvento.MiEvento += new EventHandler(ObjetoDeLaClaseQueDefineElEvento_MiEvento);
...
protected void ObjetoDeLaClaseQueDefineElEvento_MiEvento(object sender, EventArgs e)
{
    //manejar el evento
}

I hope that this (too extensive) answer has clarified the subject a bit.

    
answered by 12.07.2018 в 11:17
1

Once you have what you have described, you only have to declare your event, you can generate it automatically by typing this.btnAceptar.Click += and pressing in this point 2 times the tab key, or you can write it by hand;

    private void btnAceptar_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

But the event that you assign to your control is indicated in btnAceptar.Click this will be activated when you click on the control, since you yourself specified Click .

Depending on the type of control you work with, the different events that you can assign vary.

    
answered by 11.07.2018 в 16:49