Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change WinForms combo box selection with UI Automation

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).

like image 913
NoizWaves Avatar asked Oct 15 '22 17:10

NoizWaves


1 Answers

    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();
    }
like image 55
sizu Avatar answered Oct 20 '22 19:10

sizu