Help with checkboxes

Ask general questions here.
rtest
Posts: 2
Joined: Fri Aug 05, 2016 9:18 pm

Help with checkboxes

Post by rtest » Fri Aug 05, 2016 9:38 pm

Hi all,

How do I automate checking/unchecking a checkbox based on if the checkbox is checked or not. I have searched the forum and cannot find the information I need. I am automating using user code.
Basically, I want to iterate through all the checkboxes and check/uncheck a checkbox based on its status.
This is a windows application. I am on version 5.4.6.
I am new to Ranorex but not to automation

Thanks

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Help with checkboxes

Post by Support Team » Mon Aug 08, 2016 2:04 pm

Hi rtest,

Welcome to Ranorex :)

To overcome your intention, you could use different approaches. For example:
var checkBoxList = repo.Window.Self.Find<CheckBox>(".//Checkbox");
foreach(var checkBoxItem in checkBoxList)
{
checkBoxItem.Checked= !checkBoxItem.Checked;
}
The same structure can also be used with a mouse click operation
var checkBoxList = repo.Window.Self.Find<CheckBox>(".//Checkbox");
foreach(var checkBoxItem in checkBoxList)
{
checkBoxItem.Click();
}
These loops negates the current checked attribute (all checked will be unchecked and vice versa).
repo.Window.Self is the repository item of your application and the find() method will return a list which contains all CheckBox's.
Repo_Item.png
Repo_Item.png (4.48 KiB) Viewed 631 times
Hope this helps.

Regards,
Markus (S)
.
Image

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Help with checkboxes

Post by krstcs » Mon Aug 08, 2016 2:40 pm

I know this isn't exactly what you asked, but it might be helpful in the future and is related.

I have the issue where I need to pass in a value showing whether the checkbox should be checked or not. But I need to make sure it wasn't already checked/unchecked. So the formula is:

If IsChecked = True AND NeedChecked = True then do nothing.
If IsChecked = True AND NeedChecked = False then click.
If IsChecked = False AND NeedChecked = True then click.
If IsChecked = False AND NeedChecked = False then do nothing.

This is the typical logical XOR operation (eXclusive OR), which returns TRUE ONLY IF THE OPERANDS ARE NOT THE SAME.

The code would be like this:

Code: Select all

if (repo.MyApp.MyCheckbox.Checked ^ needChecked) {
  Report.Info(<put your log info here>);
  repo.MyApp.MyCheckbox.Click();
}
Shortcuts usually aren't...

rtest
Posts: 2
Joined: Fri Aug 05, 2016 9:18 pm

Re: Help with checkboxes

Post by rtest » Tue Aug 09, 2016 10:25 pm

Thanks, it worked !!