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

How to find out if a check box is checked...

tbarto
P: 19
Hi everybody

For some time I have been working on an UI testing application. The application under test was developed in C#, and my application has been developed in C# as well. I do not have a source code of the application under test, so I use Win API messages and methods. I faced the following problem:

The application under test contains a check box:

Class: WindowsForms10.BUTTON
Text: "Some text"
Style: "Some styles"

After I get a handle to the check box ( I am sure I have the correct handle ) I use SendMessage to retrieve the check box state:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. private const int BM_GETSTATE       = 0x00F2;
  4. private const int BM_GETCHECK       = 0x00F0;
  5. private const int BST_UNCHECKED     = 0x0000;
  6. private const int BST_CHECKED       = 0x0001;
  7.  
  8. int result;
  9.  
  10. result = SendMessage(correctHandle, BM_GETSTATE, 0, 0);
  11.  
  12. or
  13.  
  14. result = SendMessage(correctHandle, BM_GETCHECK, 0, 0);
  15.  
  16.  
My problem is that both SendMessage methods from above always return 0, even if a check box is checked.

Can anybody tell me what is wrong with the code above?

Thanks in advance.
Apr 24 '07 #1
Share this Question
Share on Google+
22 Replies


P: 5
Hi tbarto,
Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.


Hi everybody

For some time I have been working on an UI testing application. The application under test was developed in C#, and my application has been developed in C# as well. I do not have a source code of the application under test, so I use Win API messages and methods. I faced the following problem:

The application under test contains a check box:

Class: WindowsForms10.BUTTON
Text: "Some text"
Style: "Some styles"

After I get a handle to the check box ( I am sure I have the correct handle ) I use SendMessage to retrieve the check box state:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. private const int BM_GETSTATE       = 0x00F2;
  4. private const int BM_GETCHECK       = 0x00F0;
  5. private const int BST_UNCHECKED     = 0x0000;
  6. private const int BST_CHECKED       = 0x0001;
  7.  
  8. int result;
  9.  
  10. result = SendMessage(correctHandle, BM_GETSTATE, 0, 0);
  11.  
  12. or
  13.  
  14. result = SendMessage(correctHandle, BM_GETCHECK, 0, 0);
  15.  
  16.  
My problem is that both SendMessage methods from above always return 0, even if a check box is checked.

Can anybody tell me what is wrong with the code above?

Thanks in advance.
Apr 27 '07 #2

tbarto
P: 19
Hi tbarto,
Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.
Hi amitjul

As far as now I haven't found the solution. I've been working on some other issues. If I am able to get the solution, I will post it here.

Regards
Apr 27 '07 #3

SammyB
Expert 100+
P: 807
Hi tbarto,
Me too finding the same problem..if you got its solution, please post it here...i'll be very thankful if you give some time for that.
Googling for SendMessage, I found out the the Net checkbox will always return 0 because it has an extended style of '0000000B'. He later posted that he solved the problem with AccessibleObjectFromWindow but that it was difficult because of poor documentation. He did not post a solution :mad:

AccessibleObjectFromWindow documentation is http://msdn2.microsoft.com/en-us/library/ms696137.aspx

Finally, googling AccessibleObjectFromWindow "C#" shows some working code, but very ugly. If this helps you get a solution, please post it here. Thanks! --Sam
Apr 27 '07 #4

tbarto
P: 19
Gentleman

I have found how to get access to an UI component with AccessibleOblectFromWindow, however it still does not work for me:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private const int OBJID_WINDOW = 0;
  3.  
  4. [DllImport("oleacc.dll", PreserveSig = false)]
  5. [return: MarshalAs(UnmanagedType.Interface)]
  6. public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);
  7.  
  8. Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
  9.  
  10. IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
  11. ref uid);
  12.  
  13. object o = accObj.get_accValue(0); //always 0
  14.  
  15. object o1 = accObj.get_accName(0); // works fine
  16.  
  17.  
About the code above: the get_accName works fine for me, it returns a text that the checxbox contains. Unfortunately, get_accValue always returns 0, even if the checkbox is checked. Perhaps any of You can see that there is something wrong?
Apr 30 '07 #5

SammyB
Expert 100+
P: 807
Gentleman

I have found how to get access to an UI component with AccessibleOblectFromWindow, however it still does not work for me:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private const int OBJID_WINDOW = 0;
  3.  
  4. [DllImport("oleacc.dll", PreserveSig = false)]
  5. [return: MarshalAs(UnmanagedType.Interface)]
  6. public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);
  7.  
  8. Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
  9.  
  10. IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
  11. ref uid);
  12.  
  13. object o = accObj.get_accValue(0); //always 0
  14.  
  15. object o1 = accObj.get_accName(0); // works fine
  16.  
  17.  
About the code above: the get_accName works fine for me, it returns a text that the checxbox contains. Unfortunately, get_accValue always returns 0, even if the checkbox is checked. Perhaps any of You can see that there is something wrong?
So each control has a GUID? How do you figure out what it is?
Apr 30 '07 #6

tbarto
P: 19
So each control has a GUID? How do you figure out what it is?
Hi

This is what I've been trying to figure out. I have found that:

Expand|Select|Wrap|Line Numbers
  1. Guid g = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
when googling, but I do not exactly understand the "618736e0-3c3d-11cf-810c-00aa00389b71" identifier. I have tried to change that identifier and run the code, but it caused an exception. Since I am not experienced in working with "Guid" I did not find the answer for Your question so far.
Apr 30 '07 #7

SammyB
Expert 100+
P: 807
> I have found that:Guid when googling
Hopefully, not at http://www.cafepress.com/cp/moredeta...olorNo=23&pr=F
Apr 30 '07 #8

tbarto
P: 19
> I have found that:Guid when googling
Hopefully, not at http://www.cafepress.com/cp/moredeta...olorNo=23&pr=F
No, definitely not that one :-)
Apr 30 '07 #9

SammyB
Expert 100+
P: 807
No, definitely not that one :-)
Up until now, I have associated GUIDs with tools that are in the registry and used code such as http://msdn2.microsoft.com/en-us/lib...id(vs.80).aspx. But, it looks like from http://msdn2.microsoft.com/en-us/lib...29(VS.80).aspx that there maybe a GUID for each control. It looks like http://msdn2.microsoft.com/en-us/library/ms797957.aspx may be some help in finding the GUIDs. What a rat's nest!
Apr 30 '07 #10

tbarto
P: 19
Up until now, I have associated GUIDs with tools that are in the registry and used code such as http://msdn2.microsoft.com/en-us/lib...id(vs.80).aspx. But, it looks like from http://msdn2.microsoft.com/en-us/lib...29(VS.80).aspx that there maybe a GUID for each control. It looks like http://msdn2.microsoft.com/en-us/library/ms797957.aspx may be some help in finding the GUIDs. What a rat's nest!
Thanks for the info You've provided. I will try work on that, unfortunately next week, because tomorrow my vacation days start :-)
Thanks one more time for Your help
Apr 30 '07 #11

P: 5
Hi Guys,
That Guid is the constant which you can't change (i dont know the reason :) ). and AccessibleObjectFromWindow is working with preety weird way. i tried to decode it but i failed. ne way, i just found a good solution.

IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
ref uid);

In above code, instead of passing OBJID_WINDOW , try with OBJID_CLIENT

accObj.get_accState(0) will return you some integer value like as below:

public const long UNCHECKED = 1048576;
public const long CHECKED = 1048592;
public const long UNCHECKED_FOCUSED = 1048580; // if control is focused
public const long CHECKED_FOCUSED = 1048596; // if control is focused


so based on that you can easily distiguish that which one is checked (checkbox/radionbutton). Its weird but deadlines are there so.... :)





Thanks for the info You've provided. I will try work on that, unfortunately next week, because tomorrow my vacation days start :-)
Thanks one more time for Your help
May 3 '07 #12

SammyB
Expert 100+
P: 807
Hi Guys,
That Guid is the constant which you can't change (i dont know the reason :) ). and AccessibleObjectFromWindow is working with preety weird way. i tried to decode it but i failed. ne way, i just found a good solution.

IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
ref uid);

In above code, instead of passing OBJID_WINDOW , try with OBJID_CLIENT

accObj.get_accState(0) will return you some integer value like as below:

public const long UNCHECKED = 1048576;
public const long CHECKED = 1048592;
public const long UNCHECKED_FOCUSED = 1048580; // if control is focused
public const long CHECKED_FOCUSED = 1048596; // if control is focused


so based on that you can easily distiguish that which one is checked (checkbox/radionbutton). Its weird but deadlines are there so.... :)
So, it all came out guid and you get the t-shirt.! :D
May 3 '07 #13

Plater
Expert 5K+
P: 7,872
Are you trying to capture the checked state of a checkbox in another application that is not your own? Sneaky sneaky.
May 3 '07 #14

P: 5
I am trying to get the state of checkbox/radiobutton/button from a C# application to C# application. so both are different application and both are mine. :)


Where is the T-shirt ?? :)

Are you trying to capture the checked state of a checkbox in another application that is not your own? Sneaky sneaky.
May 4 '07 #15

P: 5
Hi Guys, does anyone knows how to fire any keys. I mean if i want to press ESC key to unload one particular dialog/form How to send message using API in C# ??

I am trying to get the state of checkbox/radiobutton/button from a C# application to C# application. so both are different application and both are mine. :)


Where is the T-shirt ?? :)
May 4 '07 #16

P: 5
No one knows ???? :(
Hi Guys, does anyone knows how to fire any keys. I mean if i want to press ESC key to unload one particular dialog/form How to send message using API in C# ??
May 7 '07 #17

tbarto
P: 19
No one knows ???? :(
Hi Amitjul

Here is probably a solution You've been looking for. You may use:

System.Windows.Forms.SendKeys.SendWait(keys);

method in the follwing way:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. [DllImport("user32.dll")]
  4. [return: MarshalAs(UnmanagedType.Bool)]
  5. private static extern bool SetForegroundWindow(IntPtr hWnd);
  6.  
  7.  
  8. IntPtr _handle;  //handle to the destination control
  9.  
  10. public void SendKeys(string keys, int delay) {
  11.  
  12.  
  13. if(!(SetForegroundWindow(_handle)))
  14.     throw new ExternalException("SetForegroundWindow failed");
  15.  
  16. System.Windows.Forms.SendKeys.SendWait(keys);
  17. Thread.Sleep(delay);            
  18. }
  19.  
  20.  
More info about the keys that can be sent to an active control and about SendKeys.SendWait(keys) method You will find here:

http://msdn2.microsoft.com/en-us/lib....sendkeys.aspx
May 7 '07 #18

tbarto
P: 19
Hi Guys,
That Guid is the constant which you can't change (i dont know the reason :) ). and AccessibleObjectFromWindow is working with preety weird way. i tried to decode it but i failed. ne way, i just found a good solution.

IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(hwnd, OBJID_WINDOW,
ref uid);

In above code, instead of passing OBJID_WINDOW , try with OBJID_CLIENT

accObj.get_accState(0) will return you some integer value like as below:

public const long UNCHECKED = 1048576;
public const long CHECKED = 1048592;
public const long UNCHECKED_FOCUSED = 1048580; // if control is focused
public const long CHECKED_FOCUSED = 1048596; // if control is focused


so based on that you can easily distiguish that which one is checked (checkbox/radionbutton). Its weird but deadlines are there so.... :)
Hi

When running the code i get 0 when a checkbox is unchecked and 16 when it is checked. The values are not exactly the same as in Your code, however I am able to determine if a checkbox is checked or not.

Thanks a lot!!!
May 7 '07 #19

tbarto
P: 19
Hi

When running the code i get 0 when a checkbox is unchecked and 16 when it is checked. The values are not exactly the same as in Your code, however I am able to determine if a checkbox is checked or not.

Thanks a lot!!!
Sorry,

My fault. Amitjul - everything works exactly as in Your code.

Thanks
May 7 '07 #20

tbarto
P: 19
I was just wondering. Perhaps any of You Guys knows a way to get access to the PropertyGrid component? The problem is that I am unable to find an info about that. E.g:

http://msdn.microsoft.com/library/de...divcontrol.asp

Under the link above there is an info about many UI components, unfortunately nothing about PropertyGrid :-(
May 7 '07 #21

tbarto
P: 19
I have found some differences in types on object returend by AccessibleObjectFromWindow. What I mean is that when working on Win XP, the type of the object is System._ComObject , but as I tried on Win 2000 the type was System.Runtime.Remoting.Proxies._TransparentProxy . Obviously, I was unable to cast that type to IAccessible, which caused an exception. Does anybody have any idea about those differences?

Thanks
May 7 '07 #22

P: 1
Hello guys,
I'm working in .net C# to get the URL from IE and Firefox. The IE part was over and I'm stuck up at firefox.
I can't drill down to its address bar(combobox) to get its value. So can anybody help me this problem.
I've gone through your above discussion but not getting what I want.
Ive used accExplorer 2.0 to see the hierarchy of the firefox, and I can see the address bar there but problem is how to access it.
Mar 20 '08 #23

Post your reply

Sign in to post your reply or Sign up for a free account.