Ottenere lo stato di attivazione/disattivazione di una casella di controllo utilizzando l'automazione interfaccia utente

Nota

Questa documentazione è destinata agli sviluppatori .NET Framework che desiderano utilizzare le classi di UI Automation gestite definite nello spazio dei nomi System.Windows.Automation. Per informazioni aggiornate su UI Automation, vedere API di automazione di Windows: UI Automation.

Questo argomento illustra come usare Microsoft UI Automation per ottenere lo stato di attivazione/disattivazione di un controllo.

Esempio

In questo esempio viene utilizzato il metodo GetCurrentPattern della classe AutomationElement per ottenere un oggetto TogglePattern da un controllo e restituirne la proprietà ToggleState.

/// <summary>
/// Gets the toggle state of an element in the target application.
/// </summary>
/// <param name="element">The target element.</param>
private bool IsElementToggledOn(AutomationElement element)
{
    if (element == null)
    {
        // TODO: Invalid parameter error handling.
        return false;
    }

    Object objPattern;
    TogglePattern togPattern;
    if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
    {
        togPattern = objPattern as TogglePattern;
        return togPattern.Current.ToggleState == ToggleState.On;
    }
    // TODO: Object doesn't support TogglePattern error handling.
    return false;
}
''' <summary>
''' Gets the toggle state of an element in the target application.
''' </summary>
''' <param name="element">The target element.</param>
Private Function IsElementToggledOn(ByVal element As AutomationElement) As Boolean
    If element Is Nothing Then
        ' TODO: Invalid parameter error handling.
        Return False
    End If

    Dim objPattern As Object = Nothing
    Dim togPattern As TogglePattern
    If True = element.TryGetCurrentPattern(TogglePattern.Pattern, objPattern) Then
        togPattern = TryCast(objPattern, TogglePattern)
        Return togPattern.Current.ToggleState = ToggleState.On
    End If
    ' TODO: Object doesn't support TogglePattern error handling.
    Return False
End Function