Simulating button pres by another button press

M

Mirs

I have a WinForm and a few buttons. I want to simulate a button press of a
certain button by pressing on a second one. I can copy the code inside the
simulated button into the second but I don't know what to do with the
parameters (object sender, EventArgs e) that the system use when it calls
the method.
Can someone advise please?
Regards
Mirs
 
Z

zacks

I have a WinForm and a few buttons. I want to simulate a button press of a
certain button by pressing on a second one. I can copy the code inside the
simulated button into the second but I don't know what to do with the
parameters (object sender, EventArgs e) that the system use when it calls
the method.
Can someone advise please?
Regards
Mirs

WHy not use the Button.PerformClick() method?
 
F

Family Tree Mike

Mirs said:
I have a WinForm and a few buttons. I want to simulate a button press of a
certain button by pressing on a second one. I can copy the code inside the
simulated button into the second but I don't know what to do with the
parameters (object sender, EventArgs e) that the system use when it calls
the method.
Can someone advise please?
Regards
Mirs

In addition to Button.PerformClick(), you can just call the click event
handler, passing a null for each parameter, unless you actively check those
parameters for some reason.
 
J

Jeff Johnson

I have a WinForm and a few buttons. I want to simulate a button press of a
certain button by pressing on a second one. I can copy the code inside the
simulated button into the second but I don't know what to do with the
parameters (object sender, EventArgs e) that the system use when it calls
the method.
Can someone advise please?

The standard event-driven advice is that your event handlers should do
NOTHING other than call a separate method which actually runs the code. (I
admit I don't always follow this advice, although I do agree with it in
principle for all but the most trivial code.)

In other words, your code should look like this:

private void lookupButton_click(object sender, EventArgs e)
{
HandleLookup();
}

private void HandleLookup()
{
// All the important stuff here
}

Now if you want to "click" the button, i.e., run the code that a button
click invokes, you simply call HandleLookup() instead of trying to simulate
a click on the button.

The only time this wouldn't work would be if you were trying to VISUALLY
click the button, i.e., press it down and release it. That's more difficult
and requires P/Invoke.
 
P

proxyuser

Jeff Johnson said:
The only time this wouldn't work would be if you were trying to VISUALLY
click the button, i.e., press it down and release it. That's more
difficult and requires P/Invoke.

Can you expand? button.PerformClick() does not visually click the button.
What is the mechanism you're talking about?
 
J

Jeff Johnson

Can you expand? button.PerformClick() does not visually click the button.
What is the mechanism you're talking about?

Sending a BM_SETSTATE message to the button with a wParam of TRUE (1) to
"press" the button and FALSE (0) the "unpress" it. Of course, you have to
throw a delay timer in there or the user will never see the button in the
pressed state.
 

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