How to raise events programatically?

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

Michael Lang

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
 
A

AlexB

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
 
M

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

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
 

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