簡體   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