Reputation: 2680
is it possible to change the selected item in a winforms application using c# UI automation (same logic as UIspy.exe)? I would like to change the selected item to a specific item (I know it's index/position in the list).
Upvotes: 5
Views: 5480
Reputation: 41
Further to sizu's answer - You need to expand the ComboBox BEFORE retrieving the Items. The items only "exist" as far as UIAutomation is concerned when the ComboBox has been expanded.
Upvotes: 1
Reputation: 11
How can we set the value in a Combo box where the list is generated dynamically.
Like in my case
AutomationElementCollection comboboxItem = comboBoxElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
comboboxItem.Count is 0
While in the same combobox when i expand it using mouse click it shows all the values.
Upvotes: 1
Reputation: 126
public static void ActionSelectComboBoxItem(AutomationElement comboBoxElement, int indexToSelect){
if(comboBoxElement == null)
throw new Exception("Combo Box not found");
//Get the all the list items in the ComboBox
AutomationElementCollection comboboxItem = comboBoxElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
//Expand the combobox
ExpandCollapsePattern expandPattern = (ExpandCollapsePattern)comboBoxElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);
expandPattern.Expand();
//Index to set in combo box
AutomationElement itemToSelect = comboboxItem[indexToSelect];
//Finding the pattern which need to select
SelectionItemPattern selectPattern = (SelectionItemPattern)itemToSelect.GetCurrentPattern(SelectionItemPattern.Pattern);
selectPattern.Select();
}
Upvotes: 2