PC Review


Reply
Thread Tools Rate Thread

Programatically pressing a button

 
 
Jared Miniman
Guest
Posts: n/a
 
      17th Mar 2005
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


 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      18th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:u9H$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Jared Miniman
Guest
Posts: n/a
 
      18th Mar 2005
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]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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" <(E-Mail Removed)> wrote in message
> news:u9H$(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pressing the button without... pressing the button - mouse click? efandango Microsoft Access Form Coding 2 18th Dec 2008 11:04 PM
Pressing button ReneMarxis Microsoft C# .NET 6 11th Sep 2007 03:05 AM
Require pressing of button =?Utf-8?B?TmluaWVs?= Microsoft Access 11 10th Oct 2006 09:05 PM
Error when pressing the OK button =?Utf-8?B?Sm9l?= Microsoft Access Forms 2 21st Mar 2005 09:47 PM
Pressing the back button =?Utf-8?B?QmVu?= Windows XP Internet Explorer 2 25th Dec 2004 01:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 PM.