how to raise the click event by code

  • Thread starter Thread starter Fanor
  • Start date Start date
Hi Fanor,

If your control derives from either UserControl or Control you can call
the protected OnClick method from within your control's definition:

this.OnClick(EventArgs.Empty);

or, in the case you need to make this method public to consumers of
your control you can expose a public interface to the protected
"OnClick" method (the Button control already does this):

public void PerformClick()
{
this.OnClick(EventArgs.Empty);
}

Additionally, you can override the OnClick method in your derived
control's implementation (so your control can process other things
before raising the "Click" event).

protected override void OnClick(EventArgs e)
{
//do something here before calling the base OnClick() method to
raise the click event.
base.OnClick (e);
}


-GM
http://nonspect.com
 
public void PerformClick()
{
this.OnClick(EventArgs.Empty);
}

And if you already have a System.Windows.Forms.Button control, then a
public PerformClick() method is already available.

Markus
 
Thanks guys,

But my problem is a bit different. Usually the click event is raised when
the user push a mouse button, what I want to do is to send this "click" by
code to a button control in order to raise the click event of that button.

TIA
 
....myButton.PerformClick();

Thanks guys,

But my problem is a bit different. Usually the click event is raised when
the user push a mouse button, what I want to do is to send this "click" by
code to a button control in order to raise the click event of that button.

TIA
 
Fanor,

Try Control.InvokeOnClick method.

It is protected method of the Control class, so it needs to be called from a
class that derives from a Control. However using this method you can raise
Click event on any other control.
 
Back
Top