How to raise events programatically?

  • Thread starter Thread starter asr
  • Start date Start date
A

asr

I've a form with a button on it.

How to raise (I mean to run the code within the click
event of the button) without clicking the button?
Is it possible to raise the events programatically.
 
I've a form with a button on it.

How to raise (I mean to run the code within the click
event of the button) without clicking the button?
Is it possible to raise the events programatically.

You can only raise an event from within the class itself (or a derived
class).

If you have a common method that should be run both on the click of a
button and some other instance, then put all that code within a new method.
You can call that method from both the click event and your other method...

===================================
private void Button1_Click(...)
{
SharedClickLogic();
}
private void YourOtherMethod(...)
{
SharedClickLogic();
}
private void SharedClickLogic()
{
//do stuff here
}
===================================

Michael Lang
 
Just call the name of the sub-routine in your code the way
you call any other sub, e.g.

call Button1_Click()

or

Button1_click
 
Just call the name of the sub-routine in your code the way
you call any other sub, e.g.

call Button1_Click()

or

Button1_click

Ah, but now you have to pass the sender and eventArgs which you didn't
mention in your sample.

Button1_Click(null, null); //bad practice

Be careful with this route. Creating a new common method is more clear
(see my other post).

Michael Lang, MCSD
 
Back
Top