button_click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is this the correct code to have one button execute the code of anothe rbutton?

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printToolStripMenuItem.Click += new EventHandler(
buttonLoad_Click);
}
 
A button's "code" is just a method. You can just call the method directly
if you need it.
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
buttonLoad_Click();
}
 
I get an error:
No overload for buttonLoad_Click(); takes 0 arguments
 
You'll need to supply arguments for the arguments that the code you are
calling is expecting.

You can pass the current arguments to the called method like this:
 
Chris said:
Is this the correct code to have one button execute the code of anothe rbutton?

private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printToolStripMenuItem.Click += new EventHandler(
buttonLoad_Click);
}

That code would add an event handler every time you click the button.
That means that the first time you click the button, the method is not
called, the second time you click the button the method is called once,
the thrird time you click the button the method is called twice, and so on.

Just change the Click property of the button to use the buttonLoad_Click
method instead of the printToolStripMenuItem_Click method.

Perhaps also you should change the name of the method to reflect what it
actually does instead of buttonLoad_Click, as it no longer only handles
that event.
 

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

Back
Top