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).
    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();
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With