how to raise the click event by code

F

Fanor

Hi all,
I want to raise the event click of a control by code. How can i do that ??
 
G

GM3TEN

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
 
M

Markus

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
 
F

Fanor

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
 
G

GM3TEN

....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
 
S

Stoitcho Goutsev \(100\)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top