Can I fire another event from within an event?

  • Thread starter Thread starter OC
  • Start date Start date
O

OC

I would like to trigger another event from within an event that is specific
to a forms state at the time of the original event.

Is this possible?
 
OC said:
I would like to trigger another event from within an event that is specific
to a forms state at the time of the original event.

Is this possible?

Sure, there is no reason the handler for an event can't fire another event.
 
Sure. For instance, lets say you want to fire the click event for button2
from the click event for button1 you could do this:
 
Hi Kirk,

Kirk Graves said:
Sure. For instance, lets say you want to fire the click event for button2
from the click event for button1 you could do this:

-------------------------
private void button1_Click(object sender, System.EventArgs e)
{
button2_Click(this.button2,e);
}
------------------------
or, to be a little more honest, you could do this this way
------------------------
private void button1_Click(object sender, System.EventArgs e)
{
button2_Click(sender,e);
}
-----------------------

The above code is actually calling another method that may or may not
also be an event handler for button2's click event. If you actually wanted
to raise a given event for a control, you would call the appropriate
On[EventName] method. Of course the On[EventName] methods are protected,
which means you can only do this from the class itself or an inheriting
class. One exception to this rule happens to be the Button control, which
has a public PerformClick method (which does little more than call the
protected OnClick method).

Regards,
Dan
 
Back
Top