By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

Simulate an event

P: n/a
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?
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"johnny" <jo****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
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.

--
Michael Culley
Nov 16 '05 #2

P: n/a
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" wrote:
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?

Nov 16 '05 #3

P: n/a

"johnny" <jo****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
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
Nov 16 '05 #4

P: n/a
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.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"johnny" <jo****@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
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?

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.