Answered by:
Index was out of range.Must be non-negative and less than the size of the collection

Question
-
Any idea how to fix this issue. Thanks
string[] projectNames = { "X_Standard_Project", "Y_Standard_Project" };
for (int i = 0; i < projectNames.Length; i++)
{
this.UIMap.Modified_RightclickLibraryProjandSelectUse(projectNames[i]);
}public void Modified_RightclickLibraryProjandSelectUse(string projectName)
{
#region Variable Declarations
WinTreeItem uIX_Standard_Project_TreeItem = this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem.UIX_Standard_Project_TreeItem;
WinMenuItem uIUseMenuItem = this.UIItemWindow2.UIContextMenu.UIUseMenuItem;
#endregion
WinTreeItem UISearchTreeItem = new WinTreeItem(this.UIControlGridWindow1.UIItemWindow1);
UISearchTreeItem.SearchProperties[WinTreeItem.PropertyNames.Name] = projectName + "_Test";
UITestControlCollection UISearchReslts = UISearchTreeItem.FindMatchingControls();// It finds the control in the tree list and the count should be 1
WinTreeItem UIWantedTreeItem = UISearchReslts[UISearchReslts.Count - 1] as WinTreeItem; //fails as count is 0.}
- Edited by tsingh2 Wednesday, December 11, 2013 11:06 AM
Wednesday, December 11, 2013 1:13 AM
Answers
-
1- Please cross Check which is the WinTree Control for TreeItems.
if UIProjectsTreeItem is not a WinTree Controls then Try to use
WinTreeItem UIWantedTreeItem = new WinTreeItem(this.UITrilliumControlCenteWindow1.UIItemWindow1);
2- When you are adding search Property "Name" then it is expected to get only one...
3- If you are looking for all the TreeItems then try GetChildren() for WinTree Control.
Thanks
Brajpal
- Edited by Brajpal Jangir Thursday, December 12, 2013 11:34 AM
- Proposed as answer by griswald_911 Thursday, December 12, 2013 12:21 PM
- Marked as answer by tsingh2 Thursday, December 12, 2013 8:49 PM
Thursday, December 12, 2013 11:33 AM
All replies
-
tsingh2,
if this line:
UITestControlCollection UISearchReslts = UISearchTreeItem.FindMatchingControls();
is supposed to return a collection that contains 1 item but it fails and doesn't find any controls,
then this line:
WinTreeItem UIWantedTreeItem = UISearchReslts[UISearchReslts.Count - 1] as WinTreeItem;
will throw an index out of range exception because UISearchReslts.Count is already 0 and the indexer must be a zero-based, non-negative integer.
In other words,
0 - 1 = -1 and you can't use -1 as value for an index, i.e. UISearchReslts[-1]...
Hope it helps,
- Edited by griswald_911 Wednesday, December 11, 2013 5:35 PM
Wednesday, December 11, 2013 5:33 PM -
Thanks Griswald..But your solution doesn't help.
I still get error "Index out of range"
Wednesday, December 11, 2013 6:43 PM -
Put a breakpoint just after this line:
UITestControlCollection UISearchReslts = UISearchTreeItem.FindMatchingControls();
While the execution has paused, hover over UISearchReslts.
If the Count property is equal to 0, the code that follows will always fail.
If you give us an idea of what you are trying to accomplish, we may be able to provide sample code.
Wednesday, December 11, 2013 7:13 PM -
Thanks,
Like I mentioned in my original comment
I am passing project name "X_Standard_Project" and adding string "_Test" to make it "X_Standard_Project_Test"
In the called function based on the UI Tree view we have list of project under it like "X_Standard_Project_Test" and
"Y_Standard_Project_Test" , so I expect it to find the project "X_Standard_Project_Test" in the list.
Wednesday, December 11, 2013 7:41 PM -
The search will be more likely to find something if you specify the WinTree control that should contain the WinTreeItem you are looking for, instead of using "UIItemWindow1":
WinTreeItem UISearchTreeItem = new WinTreeItem(YourWinTree);
Also, if any part of the tree needs to be expanded, you will need to do that recursively.
Wednesday, December 11, 2013 9:15 PM -
It does not work when my pass it as follows:
WinTreeItem UISearchTreeItem = new WinTreeItem(uIX_Standard_Project_TreeItem);
Wednesday, December 11, 2013 9:25 PM -
According to your code, uIX_Standard_Project_TreeItem is a WinTreeItem and not a WinTree. You need to find the WinTree control that is a parent to your WinTreeItem.Wednesday, December 11, 2013 11:03 PM
-
Your shared code shows that the New WinTreeItem object you are creating is not in the Correct Hierarchy. Please see the method below...
public void Modified_RightclickLibraryProjandSelectUse(string projectName)
{
#region Variable Declarations
WinTreeItem uIX_Standard_Project_TreeItem = this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem.UIX_Standard_Project_TreeItem;
WinMenuItem uIUseMenuItem = this.UIItemWindow2.UIContextMenu.UIUseMenuItem;
#endregionWinTreeItem UISearchTreeItem = new WinTreeItem(this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem);
UISearchTreeItem.SearchProperties[WinTreeItem.PropertyNames.Name] = projectName + "_Test";UITestControlCollection UISearchReslts = UISearchTreeItem.FindMatchingControls();// It finds the control in the tree list and the count should be 1
WinTreeItem UIWantedTreeItem = UISearchReslts[UISearchReslts.Count - 1] as WinTreeItem; //fails as count is 0.}
You are expecting to get always one TreeItem with Specified Name so instead of using FindMatchingControls() you should use only Find() or go for any Property Check so this Control would be searched on UI.
public void Modified_RightclickLibraryProjandSelectUse(string projectName)
{
#region Variable Declarations
WinTreeItem uIX_Standard_Project_TreeItem = this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem.UIX_Standard_Project_TreeItem;
WinMenuItem uIUseMenuItem = this.UIItemWindow2.UIContextMenu.UIUseMenuItem;
#endregionWinTreeItem UIWantedTreeItem = new WinTreeItem(this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem);
UIWantedTreeItem.SearchProperties[WinTreeItem.PropertyNames.Name] = projectName + "_Test";UIWantedTreeItem.Find(); // This returns the True and Pass if match found else it would throw the error...
}
Thanks
Brajpal
Thursday, December 12, 2013 4:57 AM -
Hi Brajpal,
You suggestion does find the item, but it always find the first item in the tree. It does not increment in sequence
for the rest of the items in the tree list.
Any suggestions
Thursday, December 12, 2013 10:59 AM -
1- Please cross Check which is the WinTree Control for TreeItems.
if UIProjectsTreeItem is not a WinTree Controls then Try to use
WinTreeItem UIWantedTreeItem = new WinTreeItem(this.UITrilliumControlCenteWindow1.UIItemWindow1);
2- When you are adding search Property "Name" then it is expected to get only one...
3- If you are looking for all the TreeItems then try GetChildren() for WinTree Control.
Thanks
Brajpal
- Edited by Brajpal Jangir Thursday, December 12, 2013 11:34 AM
- Proposed as answer by griswald_911 Thursday, December 12, 2013 12:21 PM
- Marked as answer by tsingh2 Thursday, December 12, 2013 8:49 PM
Thursday, December 12, 2013 11:33 AM -
Hi Brajpal,
Can you provide some example of how to use getChildren()
Thanks,
Thursday, December 12, 2013 7:25 PM -
I found the solution
All I have to do is following and it search the name in the list
WinTreeItem(this.UITrilliumControlCenteWindow1.UIItemWindow1.UIProjectsTreeItem);
UIWantedTreeItem.SearchProperties[WinTreeItem.PropertyNames.Name] = projectName + "_Test";UIWantedTreeItem.DrawHighlight();
Thanks for everyone's help.
Thursday, December 12, 2013 10:36 PM