Tree view checkboxes checking progmatically

Class library usage, coding and language questions.
AdrianL
Posts: 4
Joined: Fri Aug 03, 2018 3:42 pm

Tree view checkboxes checking progmatically

Post by AdrianL » Wed Oct 03, 2018 5:22 pm

I am trying to find and check treeview nodes check boxes in a winforms 4.5 Treeview. The nodes are picked up from a variable with each main node on a separate line, but subnodes seperated by \

e.g
Node1
Node2\subnode 3

given a tree like this and wanting to select the two items node 1 and subnode 3
root

|
Node 1
|
Node 2
|
subnode 1
|
subnode 2
|
subnode3

 
  public void SelectTreeItems(string Nodes, Adapter TreeView)
        {
       		List <string> treeNodes = FileHandlers.SplitToLines(Nodes).ToList();
       		for (int i=0; i< treeNodes.Count; i++)
       		{
       			List <string> elements = treeNodes.Split('\\').ToList ();
       			StringBuilder nodeText = new StringBuilder ("container[@controlname='tableLayoutPanel1']/tree[@controlname='treeViewAttachments']/");
       			foreach (string element in elements)
       			{
       				nodeText.Append ("/checkbox[@accessiblename='");
       				nodeText.Append(element);
       				nodeText.Append("']");
       			}
       			Report.Log(ReportLevel.Info, "Find", "Finding"+nodeText.ToString());
       			RxPath rp = new RxPath (nodeText.ToString());
       			var _attachmentInfo = new RepoItemInfo(repo.FormOutlookAttachments, treeNodes , rp, 30000, null, Guid.NewGuid ().ToString ());
       			Ranorex.CheckBox checkControl = (_attachmentInfo.CreateAdapter<Ranorex.CheckBox>(true) as Ranorex.CheckBox);
       			checkControl.Click(Location.CenterLeft);
       			//checkControl.Check ();
       		}
        }


Now check moves to the middle of the item not the checkbox and the click is also too far to the right. I assume therefore I am selecting incorrectly has anyone a better way of doing this. I cannot put the nodes in the repository as they change dependent of data read in and thus have to be read from the spreadsheet of variables.
Attachments
treeviewcheckbox.rxsnp
(109.46 KiB) Downloaded 3 times

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

Re: Tree view checkboxes checking progmatically

Post by odklizec » Thu Oct 04, 2018 9:54 am

Hi,

If I'm not missing something, the solution you are trying to put together seems a bit overcomplicated to me? I would personally create a repo element from this path:
/form[@controlname='FormOutlookAttachments']/container[@controlname='tableLayoutPanel1']/tree[@controlname='treeViewAttachments']//checkbox
This xpath returns all checkboxes from treeViewAttachments element. Now you can use something like this, to enumerate all checkboxes returned by the above xpath and simply match each checkbox with string from the treeNodes list. I suppose the list contains AccessibleName for each checkbox you would like to click? If not, then you need to adapt below code.

Code: Select all

public void SelectTreeItems(string nodes, RepoItemInfo checkBoxesInfoElement)  
{
	List <string> treeNodes = FileHandlers.SplitToLines(Nodes).ToList();  
	// Create a list of adapters using the "Info" object
	List<Ranorex.CheckBox> checkBoxList = checkBoxesInfoElement.CreateAdapters<Ranorex.CheckBox>();
	foreach (Ranorex.CheckBox checkBoxItem in checkBoxList)
	{
		string checkBoxItemName = checkBoxItem .Element.GetAttributeValueText("AccessibleName");
		foreach (string treeNode in treeNodes )  
                {
			if (treeNode == checkBoxItemName)
			{
				checkBoxItem.EnsureVisible(); // usually not required, but I'm using it anyway...just to be sure the element is visible ;) 
				checkBoxItem.MoveTo();
				checkBoxItem.Click();
				break;
			}
		}
	}
}
Hope this helps? ;)
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

AdrianL
Posts: 4
Joined: Fri Aug 03, 2018 3:42 pm

Re: Tree view checkboxes checking progmatically

Post by AdrianL » Thu Oct 04, 2018 11:52 am

Thanks for the example, it has the same problem that it clicks the node but not the checkbox. and using the Check method on the hckbox does not pick it either. But it is a lot easier to find the items.

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

Re: Tree view checkboxes checking progmatically

Post by odklizec » Thu Oct 04, 2018 12:34 pm

Hi,

The thing is, that the checkbox is actually not the checkbox square with check mark, but only its name...
CheckBox.png
CheckBox.png (30.73 KiB) Viewed 100 times
What happens, if you manually click the name of check box item (i.e. not clicking the check box)? I guess it only selects the tree item with checkbox? Try to double click it instead. Another option would be to set the checkbox checked by SetValue action (setting Checked attribute to True).
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

AdrianL
Posts: 4
Joined: Fri Aug 03, 2018 3:42 pm

Re: Tree view checkboxes checking progmatically

Post by AdrianL » Tue Oct 09, 2018 10:46 am

setting the checked attribute did not work nor double clicking, but then realised that a space keypress would check it so used that.