To raise event programatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am making an windows application using c#. It contains two froms , Form1 and Form2. Form2 contains a button and its click event. I want to fire an event of click event of Form2 button from Form1. I am not getting the way for fire an event from Form1 to Form2 for button on Form2,
Can anybody help me on this .Please send me some sample code if possible.This is urgent.

Thanks
Regards
Anushree
 
Hey Anu,

Are you sure you need to be able to raise a buttons click event from another form? There must be a better way. However, if this is really what you need, then the following is what you need to do:

All controls have a protected method called OnXxx for each event. For example, the button control has a protected OnClick method. Calling the OnClick method will fire the Click event. You need to create a class that inherits the Button class to be able to call OnClick:

public class ButtonEx : Button
{
public void FireClick()
{
this.OnClick(EventArgs.Empty);
}
}

You can now create buttons of type ButtonEx on your form and fire the click event on these buttons whenever you please by calling the FireClick method on ButtonEx.

Regards, Jakob.
 
Back
Top