Изменить выбор поля со списком WinForms с помощью автоматизации пользовательского интерфейса - PullRequest
5 голосов
/ 29 января 2009

возможно ли изменить выбранный элемент в приложении winforms, используя автоматизацию пользовательского интерфейса c # (та же логика, что и в UIspy.exe)? Я хотел бы изменить выбранный элемент на определенный элемент (я знаю, что это индекс / позиция в списке).

Ответы [ 3 ]

2 голосов
/ 12 сентября 2010
    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();
    }
1 голос
/ 27 ноября 2011

В дополнение к ответу Сизу - Вам нужно расширить ComboBox ПЕРЕД получением предметов. Предметы только «существуют» настолько, насколько UIAutomation обеспокоен расширением ComboBox.

1 голос
/ 21 июля 2011

Как мы можем установить значение в поле со списком, где список генерируется динамически.

Как в моем случае

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

comboboxItem.Count равно 0

Находясь в одном и том же комбинированном окне, когда я раскрываю его с помощью щелчка мыши, отображаются все значения.

...