johnof

Hi, I have a button on a form, say 'Save' button, and want to use the code it 'triggers' (mytable.savechanges(), etc) elsewhere.

I tried triggering its associated event, MySaveButton_Click, but it requires the 'sender' and events (e) so it doesn't work, of course.

Is it possible to trigger this 'click' event from elsewhere in the code, other than by actually clicking on the button

Thank you.

John F.



Re: Windows Forms General Re-usisng button eventhandler

Mark Dawson

Hi,

do you use the sender and event object inside of the event handler, if not you can just call the handler method with null, null as the parameters since you do not use them.

Mark.






Re: Windows Forms General Re-usisng button eventhandler

Bipro

You can't actually trigger an event without the event actually occurring. Is it not possible to have another method and use that

For example:

private void MySaveButton_Click(object sender, System.EventArgs e)
{
this.DoSomethingHere();
}

private void DoSomething()
{
// TODO: add code you previously had in MySaveButton_Click(object, EventArgs)

}

private void MyMethod()
{
// this is the alternate (non-method) location from where you want to call the MySaveButton_Click code
this.DoSomething();

}

This seems pretty logical to me, however i may have missed something.

You can always do a direct call - but it's not something that i would do:

private void MyMethod()
{
EventArgs e = new EventArgs();
this.MySaveButton_Click(this, e);
}

Let me know if this helps.






Re: Windows Forms General Re-usisng button eventhandler

johnof

Thank you very much, Mark. I didn't realize -- newby that I am -- than the null arguments would work.

JohnF.





Re: Windows Forms General Re-usisng button eventhandler

johnof

Thank you, BiPro: I was looking for something elegant that didn't add a lot more code: your 'MyMethod' is a great solution.

JohnF.





Re: Windows Forms General Re-usisng button eventhandler

bipro

My pleasure. Glad i could help.