代码之家  ›  专栏  ›  技术社区  ›  NoizWaves

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

  •  5
  • NoizWaves  · 技术社区  · 17 年前

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

    3 回复  |  直到 17 年前
        1
  •  2
  •   sizu    15 年前
        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
  •  1
  •   RvdK    14 年前

    就像我的情况一样

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

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

        3
  •  1
  •   AndyM_LVB    14 年前

    在检索项目之前,需要展开组合框。这些项目仅在以下情况下“存在”