Resco
Home  |  Search  |  Login  |  Register

Home > Forums > Developer > OutlookControls for .NET CF > Click button programmatically

Click button programmatically
OutlookControls for .NET CF
Thread Starter: Qwertz
Started: 02-27-2013 4:03 AM
Replies: 4

Print Search Previous Topic Next Topic
  27 Feb 2013, 4:03 AM
Qwertz is not online. Last active: 9/9/2013 3:40:07 AM Qwertz

Not Ranked
Joined on 01-22-2013
Posts 7
Click button programmatically
Hello folks,

is there any way to click an ImageButton programmatically? Our mobile device has some hardware buttons and I'd like to click the software buttons (ImageButtons) programmatically when the user pressed one of the hardware buttons.

Thanks in advance!


  
  28 Feb 2013, 4:18 AM
Jakub Cerman is not online. Last active: 10/23/2014 6:45:01 AM Jakub Cerman

Top 10 Posts
Male
Joined on 23-02-2012
Posts 621
Re: Click button programmatically
Hi,

There is not direct way to click on the ImageButton programatically, but you can do it through System.Reflection. When you call the following code:

Type t = imageButton1.GetType();
MethodInfo mi = t.GetMethod("OnMouseDown", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(imageButton1, new object[] { new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0) });

at your hardware button mouse down handler and the following code:

Type t = imageButton1.GetType();
MethodInfo mi = t.GetMethod("OnMouseUp", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(imageButton1, new object[] { new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0) });

at your hardware button mouse up handler, it fires image button Click event programatically.



Best Regards Jakub Cerman Resco.net
 
    
  28 Feb 2013, 7:23 AM
Qwertz is not online. Last active: 9/9/2013 3:40:07 AM Qwertz

Not Ranked
Joined on 01-22-2013
Posts 7
Re: Click button programmatically
Hello Jakub,

thank you very much - it works great!

Cheers,
 Qwertz

  
  28 Feb 2013, 7:34 AM
Qwertz is not online. Last active: 9/9/2013 3:40:07 AM Qwertz

Not Ranked
Joined on 01-22-2013
Posts 7
Re: Click button programmatically
By the way:
Do you know why it is in C# possible to call "foreign" non-public methods using reflection? Shouldn't it be forbidden altogether?

 
    
  01 Mar 2013, 3:02 AM
Jakub Cerman is not online. Last active: 10/23/2014 6:45:01 AM Jakub Cerman

Top 10 Posts
Male
Joined on 23-02-2012
Posts 621
Re: Click button programmatically
It is because, in some specific cases like automated serialization, or your example above, it is necessary to access attributes, methods or events in classes and objects or obtain information about them. Ignoring access modifiers is necessary in this case.
Non public access modifiers are intended to prevent accidental misuse. It is not a security mechanism. If you choose to bypass it then you can do so at your own risk and the assumption you know what you are doing.
However, it is also a very powerful tool that can be used to inspect the internals of classes and object which can not be accessed by other standard means.



Best Regards Jakub Cerman Resco.net
  
 Page 1 of 1 (5 items)
Copyright © 2014 Resco, sro. All rights reserved.