Page 1 of 1

Help with checkboxes

Posted: Fri Aug 05, 2016 9:38 pm
by rtest
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

Re: Help with checkboxes

Posted: Mon Aug 08, 2016 2:04 pm
by Support Team
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
Hope this helps.

Regards,
Markus (S)

Re: Help with checkboxes

Posted: Mon Aug 08, 2016 2:40 pm
by krstcs
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();
}

Re: Help with checkboxes

Posted: Tue Aug 09, 2016 10:25 pm
by rtest
Thanks, it worked !!