Calling an Event

T

thomson

Hi all,
i have written codes in the button_click event, My problem is
that i need to call the same functionality written under the
button_click some where else,
One thing i can do is i can create a function and put
all the codes inside that and call. But there is additional function
call involved in it.
My question is can i use delegates to raise the event ,
If so , How it is done


Thanks in advance

thomson
 
J

J Smithers

Yup -
Here is how you do it with delegates in C# (perhaps someone else can show it
in VB.NET if necessary - the syntax is different there)

this.MyButton001.Click += new System.EventHandler(this.MyButton_Click);
this.MyButton002.Click += new System.EventHandler(this.MyButton_Click);
this.MyButton003.Click += new System.EventHandler(this.MyButton_Click);

private void MyButton_Click(object sender, System.EventArgs e) {
.... do stuff here
}
 
T

thomson

Hi,
Yeah u mapped all the button to a single click event, Here my
case is i dont have a sender, i need to invoke the
MyButton_Click(object sender, System.EventArgs e) from other part of
the code but not from a button. see we can say that how can you call
this void MyButton_Click(object sender, System.EventArgs e) from other
part of the code

Regards

thomson
 
L

Lau Lei Cheong

Why don't you do it this way, so you other function can just call
ClickHandler() if it wants...

private void MyButton_Click(object sender, System.EventArgs e)
{
Click_Handler();
}

private void Click_Handler()
{
// put your routine here
}

But if you indeed what to call it directly, I think
MyButton_Click((object)this, null) will do.
 
W

William F. Robertson, Jr.

You can just call the method directly.

private void MyButton_Click( object sender, EventArgs e )
{

}

private OtherMethod()
{
MyButtonClick( null, EventArgs.Empty );
}

It is okay for the sender parameter to be null as long as you aren't using
it in the Click Handler.

HTH,

bill
 
G

Guest

It sounds like the functionality of the button click should actually be in a
different class. Are you doing something like saving data? If so you should
probably move the logic into a seperate class that can be used in multiple
places. Post more of what you are doing for more advice.
 
T

thomson

Hi,
My issue is that i have a custom control like add, edit, delete in
the search page,
If you click the add button, it will allow you to add new
records, so iam setting some default values in the form, i usually
write the code in

protected override void OnAddDocument(object sender, System.EventArgs
e)

what happens is that after adding a new record, i have to call the same
function to set the settings

So how do i call this function

Regards

thomson
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top