使用UI自动化更改WinForms组合框选择

时间:2023-01-27 20:14:46

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

是否可以使用c#UI自动化(与UIspy.exe相同的逻辑)更改winforms应用程序中的选定项目?我想将所选项目更改为特定项目(我知道它在列表中的索引/位置)。

3 个解决方案

#1


    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();
    }

#2


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

comboboxItem.Count为0

While in the same combobox when i expand it using mouse click it shows all the values.

当我使用鼠标单击展开它时,在同一个组合框中显示所有值。

#3


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.

继sizu的答案 - 你需要在检索项目之前扩展ComboBox。当扩展ComboBox时,就UIAutomation而言,这些项目仅“存在”。

#1


    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();
    }

#2


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

comboboxItem.Count为0

While in the same combobox when i expand it using mouse click it shows all the values.

当我使用鼠标单击展开它时,在同一个组合框中显示所有值。

#3


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.

继sizu的答案 - 你需要在检索项目之前扩展ComboBox。当扩展ComboBox时,就UIAutomation而言,这些项目仅“存在”。