Freigeben über


ValuePattern.SetValue(String) Methode

Definition

Legt den Wert des Steuerelements fest.

public:
 void SetValue(System::String ^ value);
public void SetValue (string value);
member this.SetValue : string -> unit
Public Sub SetValue (value As String)

Parameter

value
String

Der festzulegende Wert. Der Anbieter für die Automatisierung der Benutzeroberfläche ist verantwortlich für das Konvertieren des Werts in den entsprechenden Datentyp.

Ausnahmen

Das Steuerelement ist schreibgeschützt.

Das Steuerelement ist nicht aktiviert.

Beispiele

Im folgenden Beispiel ist ValueProperty eine AutomationElement , die das ValuePattern Steuerelementmuster unterstützt, auf einen vom Benutzer angegebenen Wert festgelegt.

///--------------------------------------------------------------------
/// <summary>
/// Inserts a string into a text control that supports ValuePattern.
/// </summary>
/// <param name="targetControl">A text control.</param>
/// <param name="value">The string to be inserted.</param>
///--------------------------------------------------------------------
private void InsertText(AutomationElement targetControl,
                                    string value)
{
    // Validate arguments / initial setup
    if (value == null)
        throw new ArgumentNullException(
            "String parameter must not be null.");

    if (targetControl == null)
        throw new ArgumentNullException(
            "AutomationElement parameter must not be null");

    // A series of basic checks prior to attempting an insertion.
    //
    // Check #1: Is control enabled?
    // An alternative to testing for static or read-only controls 
    // is to filter using 
    // PropertyCondition(AutomationElement.IsEnabledProperty, true) 
    // and exclude all read-only text controls from the collection.
    if (!targetControl.Current.IsEnabled)
    {
        throw new InvalidOperationException(
            "The control is not enabled.\n\n");
    }

    // Check #2: Are there styles that prohibit us 
    //           from sending text to this control?
    if (!targetControl.Current.IsKeyboardFocusable)
    {
        throw new InvalidOperationException(
            "The control is not focusable.\n\n");
    }

    // Once you have an instance of an AutomationElement,  
    // check if it supports the ValuePattern pattern.
    object valuePattern = null;

    if (!targetControl.TryGetCurrentPattern(
        ValuePattern.Pattern, out valuePattern))
    {
        // Elements that support TextPattern 
        // do not support ValuePattern and TextPattern
        // does not support setting the text of 
        // multi-line edit or document controls.
        // For this reason, text input must be simulated.
    }
    // Control supports the ValuePattern pattern so we can 
    // use the SetValue method to insert content.
    else
    {
        if (((ValuePattern)valuePattern).Current.IsReadOnly)
        {
            throw new InvalidOperationException(
                "The control is read-only.");
        }
        else
        {
            ((ValuePattern)valuePattern).SetValue(value);
        }
    }
}
'''--------------------------------------------------------------------
''' <summary>
''' Inserts a string into a text control that supports ValuePattern.
''' </summary>
''' <param name="targetControl">A text control.</param>
''' <param name="value">The string to be inserted.</param>
'''--------------------------------------------------------------------
Private Sub InsertText(ByVal targetControl As AutomationElement, _
ByVal value As String)
    ' Validate arguments / initial setup
    If value Is Nothing Then
        Throw New ArgumentNullException( _
        "String parameter must not be null.")
    End If

    If targetControl Is Nothing Then
        Throw New ArgumentNullException( _
        "AutomationElement parameter must not be null")
    End If

    ' A series of basic checks prior to attempting an insertion.
    '
    ' Check #1: Is control enabled?
    ' An alternative to testing for static or read-only controls 
    ' is to filter using 
    ' PropertyCondition(AutomationElement.IsEnabledProperty, true) 
    ' and exclude all read-only text controls from the collection.
    If Not targetControl.Current.IsEnabled Then
        Throw New InvalidOperationException( _
        "The control is not enabled." + vbLf + vbLf)
    End If

    ' Check #2: Are there styles that prohibit us 
    '           from sending text to this control?
    If Not targetControl.Current.IsKeyboardFocusable Then
        Throw New InvalidOperationException( _
        "The control is not focusable." + vbLf + vbLf)
    End If

    ' Once you have an instance of an AutomationElement,  
    ' check if it supports the ValuePattern pattern.
    Dim valuePattern As Object = Nothing

    If Not targetControl.TryGetCurrentPattern( _
    valuePattern.Pattern, valuePattern) Then
        ' Elements that support TextPattern 
        ' do not support ValuePattern and TextPattern
        ' does not support setting the text of 
        ' multi-line edit or document controls.
        ' For this reason, text input must be simulated.
        ' Control supports the ValuePattern pattern so we can 
        ' use the SetValue method to insert content.
    Else
        If CType(valuePattern, ValuePattern).Current.IsReadOnly Then
            Throw New InvalidOperationException("The control is read-only.")
        Else
            CType(valuePattern, ValuePattern).SetValue(value)
        End If
    End If

End Sub

Hinweise

Einzeilige Bearbeitungssteuerelemente unterstützen den programmgesteuerten Zugriff auf ihre Inhalte über ValuePattern. Mehrzeilige Bearbeitungssteuerelemente unterstützen ValuePatterndas jedoch nicht. Stattdessen ermöglichen sie den Zugriff auf ihre Inhalte über das TextPattern Steuerelementmuster.

Gilt für:

Weitere Informationen