繁体   English   中英

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

[英]Change WinForms combo box selection with UI Automation

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

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

如何在动态生成列表的组合框中设置值。

就像我的情况一样

AutomationElementCollection comboboxItem = comboBoxElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));

comboboxItem.Count为0

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM