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.