calling a method associated to an event

  • Thread starter Thread starter Abdessamad Belangour
  • Start date Start date
A

Abdessamad Belangour

Hi all,
Suppose we have a method associated to a button object. Clicking on this
button triggers the associated method. for example : private void
btnAdd_Click(object sender, System.EventArgs e)
How can i do if i want to call it without the button been clicked ?
Thanks.
 
Abdessamad,

The method that you attach is just that, a method, so you can call it
normally, passing the parameters that you want to pass. You can pass null,
like this:

// Call the event handler.
btnAdd_Click(null, null);

But you have to be careful, because the button handler code might be
depending on those parameters. Most likely, you will want to simulate a
button click like this:

// Call the event handler.
btnAdd_Click(btnAdd, EventArgs.Empty);

Hope this helps.
 
Back
Top