Uncheck multiple CB

Best practices, code snippets for common functionality, examples, and guidelines.
yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Uncheck multiple CB

Post by yaro2o » Tue Oct 05, 2021 8:44 am

Hello I have a question. I have a list of 16 checkboxes and I need only 1 of them selected and the rest unchecked, ranorex must check that the selected cb is checked and that the rest is unchecked and if one of them is also checked then uncheck it

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Uncheck multiple CB

Post by odklizec » Tue Oct 05, 2021 8:56 am

Hi,

Please upload a Ranorex snapshot (NOT screenshot) of the UI in question (with all checkboxes). Without, at very least, snapshot it's hard to suggest something reliable.

Generally speaking, you must create a repo item with xpath, which returns all checboexs. Then you need to create a list of items, using code as described here:
https://www.ranorex.com/help/latest/han ... oryelement
Using the above code (of course modified), you can get the state of each checkbox and eventually uncheck it if checked.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Uncheck multiple CB

Post by yaro2o » Tue Oct 05, 2021 9:27 am

Attached I am sending a snap
You do not have the required permissions to view the files attached to this post.

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Uncheck multiple CB

Post by yaro2o » Tue Oct 05, 2021 9:29 am

I need to Check or uncheck only first CB column

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Uncheck multiple CB

Post by odklizec » Tue Oct 05, 2021 10:38 am

Hi,

Thanks for the snapshot. So, basically, you need to create a repo element with xpath like this:

Code: Select all

/form[@name='PraDialog']//checkbox[@instance='0']
This will return all checkboxes from left column.
Now you need to pass the repo element to UserCode like this:

Code: Select all

private void UncheckAllChecked(RepoItemInfo repoElement) 
{
    IList<Ranorex.CheckBox> checkBoxList= listItemsInfo.CreateAdapters<Ranorex.CheckBox>();
    foreach (Ranorex.CheckBox chekBoxItem in checkBoxList)
    {
        if (chekBoxItem.Element.GetAttributeValue<bool>("Checked"))
      	{
      	    Report.Info("CheckBox Item: " + chekBoxItem.Find<Ranorex.Text>("./preceding-sibling::text").Element.GetAttributeValueText("Text").ToString()); //this should return name of check box
            chekBoxItem.Click(); 
      	}
    }
}
I've not tested the code, so it may need some refinements? But generally, it should give you an idea what to do ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Uncheck multiple CB

Post by yaro2o » Tue Oct 05, 2021 11:41 am

I write something like this
IList<Ranorex.CheckBox> TCheckBoxList = 
 			repo.PraDialog.TCheckBoxInfo.CreateAdapters<Ranorex.CheckBox>(); 
 

			foreach (Ranorex.CheckBox TCheckBox in TCheckBoxList) 
				{ 
				if(TCheckBox.Checked)
				{
					TCheckBox.Click();
				}
				}
this code uncheck all CB and then I have a recording where I click on the nr of CB from variable
I had no idea or knowledge of how to do it with code
generally works :D