Simulate an event

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

Guest

On a form I would like to simulate a button click when the Enter is pressed.
My form is setup and it works if the delegate for the key pressed on the form
calls the delegate for the button pressed. But can we simulate a button click
instead?
This way if later the delegate is changed for the button, we don't forget to
do the changes on other events?
 
johnny said:
On a form I would like to simulate a button click when the Enter is pressed.
My form is setup and it works if the delegate for the key pressed on the form
calls the delegate for the button pressed. But can we simulate a button click
instead?
This way if later the delegate is changed for the button, we don't forget to
do the changes on other events?

You can put the code you want to run in a function and call it from where
ever you need to, you don't need to "simulate" a button click. But you would
be better off just setting the button as the default button.
 
The form (winform) supports a property AcceptButton. You can access it via
the properties window in the designer. You can set the acceptbutton to be
anyu button on the form. Whe enter is pressed, it will click that button.

If you phsically want to simultate a mouse click on your button, try looking
into
SendInput (Win32 not .NET) and use PInvoke.

Hope this helps!

Tom Wisnowski
MCP MCAD
 
johnny said:
On a form I would like to simulate a button click when the Enter is pressed.
My form is setup and it works if the delegate for the key pressed on the form
calls the delegate for the button pressed. But can we simulate a button click
instead?
This way if later the delegate is changed for the button, we don't forget to
do the changes on other events?

In addition to the other replies, there is a method 'PerformClick' on
Button.

I think the AcceptButton property (as Tom mentioned) is your best bet for
the enter key, however.

Stu
 
Hi johnny,

Especially for Click (and couple of others) Control class has method for
simulating it.
Take a look at Control.InvokeOnClick method.

So what you need to do is to call your form's InvokeOnClick(button,
EventArgs.Emty);

where button is reference to the button which click event you want to raise.
 
Back
Top