SelectionItemPattern.Select Method

Definition

Deselects any selected items and then selects the current element.

C#
public void Select();

Examples

The following example shows how to select an item in a container by specifying the text of the item.

C#
///--------------------------------------------------------------------
/// <summary>
/// Selects a string item in a container.
/// </summary>
/// <param name="selectionContainer">The selection container.</param>
/// <param name="itemText">The text to select.</param>
/// <remarks>
/// This deselects any currently selected items. 
/// To add the item to the current selection in a multiselect list, 
/// use AddToSelection instead of Select.
/// </remarks>
///--------------------------------------------------------------------
public void SelectListItem(
    AutomationElement selectionContainer, String itemText)
{
    if ((selectionContainer == null) || (itemText == ""))
    {
        throw new ArgumentException(
            "Argument cannot be null or empty.");
    }

    Condition propertyCondition = new PropertyCondition(
        AutomationElement.NameProperty, 
        itemText, 
        PropertyConditionFlags.IgnoreCase);

    AutomationElement firstMatch = 
        selectionContainer.FindFirst(TreeScope.Children, propertyCondition);

    if (firstMatch != null)
    {
        try
        {
            SelectionItemPattern selectionItemPattern;
            selectionItemPattern = 
            firstMatch.GetCurrentPattern(
            SelectionItemPattern.Pattern) as SelectionItemPattern;
            selectionItemPattern.Select();
        }
        catch (InvalidOperationException)
        {
            // Unable to select
            return;
        }
    }
}

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10