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

Simple question - What is the equivalent of a "Send Key" ?

P: n/a
Rob
For example, how could you programatically "click" on a button in a Form ?
Sep 29 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi Rob,

if you have some code like

Private Sub btnOK_Click(.....)
MsgBox("Hi there")
End Sub
und you want to run it anywhere else simply call this procedure like
any other procedure by its name:

btnOK_Click

If you sometimes don't want to fill in all the arguments of these
reserved event procedures change the code into:

Private Sub btnOK_Click(.....)
MyOK
End Sub

Private Sub MyOK()
MsgBox("Hi there")
End Sub

and call MyOK instead of btnOK_Click.

bye, Lorenz
Sep 29 '08 #2

P: n/a
On Sep 29, 1:00*pm, "Rob" <ro...@yahoo.comwrote:
For example, how could you programatically "click" on a button in a Form ?
Assuming you have a event sub that handles Button1.Click like:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

' Code block here when button is pressed

End Sub

....and if you want to execute the same code block inside Button1_Click
as if Button1 is clicked, to do so, you can simply try to call sub
from anywhere:

Button1_Click(sender, e)
Hope this helps,

Onur Güzel
Sep 29 '08 #3

P: n/a
Hi Rob,

"Rob" <ro***@yahoo.comwrote in message
news:68******************************@comcast.com. ..
For example, how could you programatically "click" on a button in a Form ?
Button has a PerformClick method.
Alternatively use My.Computer.Keyboard.SendKeys

Sep 29 '08 #4

P: n/a
I've normally used SendKey to send to an external application. With that in
mind, assuming you want to click a button in an external application, the
equivalent could be to use send keys to send "{ENTER}". You have to navigate
in the external app so the active button is selected. I did this with
sendkeys {TAB}. Fortunatly for me the external application was a well
defined interface, meaning, upon startup, I knew how many tabs to hit to get
to my button of interest.

If you are talking about programatically clicking a button in your own app,
the other answers are appropriate.

"Rob" wrote:
For example, how could you programatically "click" on a button in a Form ?
Sep 29 '08 #5

P: n/a
"Rob" <ro***@yahoo.comschrieb:
For example, how could you programatically "click" on a button in a Form ?
If you are talking about other processes: P/invoke + 'SendInput'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 29 '08 #6

P: n/a
Rob,

Why would you use an event when you can direct invoke the method?

\\\
Button_ClickEvent(me,nothing)
///

This works as well as the button has not the focus.

Cor

"Rob" <ro***@yahoo.comschreef in bericht
news:68******************************@comcast.com. ..
For example, how could you programatically "click" on a button in a Form ?
Sep 30 '08 #7

P: n/a
Cor Ligthert[MVP] wrote:
Why would you use an event when you can direct invoke the method?
Button_ClickEvent(me,nothing)
This works as well as the button has not the focus.
It might do, but IMHO, it's a bad habit to get into.

PerformClick() does all the same checks as clicking the button manually
(e.g. you can't PerformClick a disabled button) and you don't have to
worry about getting hold of valid EventArgs to pass to the "method"
call. OK, for Click events they're pretty simple, but just try it with
some of the /other/ events. ;-)

Also, the "sender" argument should be the Control /raising/ the event,
not the form /handling/ it, so ...

Button1_Click( Button1, EventArgs.Empty )

.... would be more correct.

HTH,
Phill W.
Sep 30 '08 #8

P: n/a
Phill,

Do what you want, I have the oposite expirience as you.

Cor

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschreef in bericht
news:gb**********@south.jnrs.ja.net...
Cor Ligthert[MVP] wrote:
>Why would you use an event when you can direct invoke the method?
> Button_ClickEvent(me,nothing)
>This works as well as the button has not the focus.

It might do, but IMHO, it's a bad habit to get into.

PerformClick() does all the same checks as clicking the button manually
(e.g. you can't PerformClick a disabled button) and you don't have to
worry about getting hold of valid EventArgs to pass to the "method" call.
OK, for Click events they're pretty simple, but just try it with some of
the /other/ events. ;-)

Also, the "sender" argument should be the Control /raising/ the event, not
the form /handling/ it, so ...

Button1_Click( Button1, EventArgs.Empty )

... would be more correct.

HTH,
Phill W.
Sep 30 '08 #9

P: n/a
On 2008-09-29, Rob <ro***@yahoo.comwrote:
For example, how could you programatically "click" on a button in a Form ?

Can you be more specific? Are you talking about a form in your application or
in another applciation? In otherwords, cross process or same process?

--
Tom Shelton
Sep 30 '08 #10

P: n/a
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschrieb:
>Why would you use an event when you can direct invoke the method?
> Button_ClickEvent(me,nothing)
>This works as well as the button has not the focus.

It might do, but IMHO, it's a bad habit to get into.

PerformClick() does all the same checks as clicking the button manually
(e.g. you can't PerformClick a disabled button) and you don't have to
worry about getting hold of valid EventArgs to pass to the "method" call.
OK, for Click events they're pretty simple, but just try it with some of
the /other/ events. ;-)

Also, the "sender" argument should be the Control /raising/ the event, not
the form /handling/ it, so ...

Button1_Click( Button1, EventArgs.Empty )

... would be more correct.
I agree. It fulfills the contract of the event. Nevertheless, I would not
call the event handler directly.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 1 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.