Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Programatically pressing a button

7 views
Skip to first unread message

Jared Miniman

unread,
Mar 17, 2005, 4:56:16 PM3/17/05
to
Seems like a simple scenario: I have a form with a single button. When the
button has focus, and the user presses a hardware key, I want to invoke the
button's onClick. In this baby case, I can just capture the KeyDown on the
one button and manually call the onClick handler with null arguments (since
I can look up in the code and find the button's OnClick delegate name).

However, what if I want to do this across an application with hundreds of
buttons? I have a component that, for each class, when a button has focus
and a specific hardware key is KeyDown'ed on it, I trap that event. Great,
I have the button's properties. I can't click it! I've tried P/Invoking
mouse_event with the button's absolute coordinates, but it merely passes a
WM_LBUTTONDN to the parent window, getting me no where. If I start using
WM_COMMAND, then I assume I need a WindowsCE.Forms.MessageWindow object
sitting inside each form, which I am trying to avoid.

Perhaps the OpenNET folks have come up with something useful in this
department. mouse_event is failing me . . .

--
_________________________________
Jared Miniman
Accenture Mobile Solutions Group


Peter Foot [MVP]

unread,
Mar 17, 2005, 7:12:14 PM3/17/05
to
A bit of reflection will get you there:-
using System.Reflection;

MethodInfo method = typeof(Control).GetMethod("OnClick",
BindingFlags.NonPublic | BindingFlags.Instance);

method.Invoke(button1, new object[]{EventArgs.Empty});

If you want to do this alot in your app then store the method info and reuse
it calling invoke with any button (or any class derived from Control).

mouse_event should also work, but the co-ordinate system requires you to
specify the co-ordinates in a scale from 0 to ffff in each direction. also
you'll need to call it twice once with left button down and again with a
left button up.

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

"Jared Miniman" <po...@tothenewsgroup.thx> wrote in message
news:u9H$cvzKFH...@tk2msftngp13.phx.gbl...

Jared Miniman

unread,
Mar 17, 2005, 11:06:04 PM3/17/05
to
Peter,

That's clever! Didn't think about using reflection, though as a general
practice, I shy away from using it. I actually got my code working by doing
something super simple: I grab the handle to the button control (made easy
by GetFocus in OpenNETCF Win32Window; we only care about focused buttons)
and the send an empty WM_LBUTTONDN/UP message to it.

Appreciate the help.

--
_________________________________
Jared Miniman
Accenture Mobile Solutions Group

"Peter Foot [MVP]" <feed...@nospam-inthehand.com> wrote in message
news:%23bQrk80...@TK2MSFTNGP12.phx.gbl...

0 new messages