共用方式為


RangeValuePattern.SetValue(Double) 方法

定義

設定與消費者介面自動化專案相關聯的值。

public:
 void SetValue(double value);
public void SetValue (double value);
member this.SetValue : double -> unit
Public Sub SetValue (value As Double)

參數

value
Double

項目的新值。

例外狀況

value 小於項目的最小值,或大於項目的最大值。

範例

在下列範例中, AutomationElement 支援控制項模式的 RangeValuePattern ,其值會設定為控制項特定的最小值。

SetRangeValue(targetControl[0], rangeValuePattern.Current.Minimum);
SetRangeValue(targetControl(0), rangeValuePattern.Current.Minimum)
///--------------------------------------------------------------------
/// <summary>
/// Sets the range value of the control of interest.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <param name="rangeValue">
/// The value (either relative or absolute) to set the control to.
/// </param>
///--------------------------------------------------------------------
private void SetRangeValue(
    AutomationElement targetControl,
    double rangeValue)
{
    if (targetControl == null)
    {
        throw new ArgumentException("Argument cannot be null.");
    }

    RangeValuePattern rangeValuePattern =
        GetRangeValuePattern(targetControl);

    if (rangeValuePattern.Current.IsReadOnly)
    {
        throw new InvalidOperationException("Control is read-only.");
    }

    try
    {
        rangeValuePattern.SetValue(rangeValue);
    }
    catch (ArgumentOutOfRangeException)
    {
        // TO DO: Error handling.
    }
    catch (ArgumentException)
    {
        // TO DO: Error handling.
    }
}
'''--------------------------------------------------------------------
''' <summary>
''' Sets the range value of the control of interest.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <param name="rangeValue">
''' The value (either relative or absolute) to set the control to.
''' </param>
'''--------------------------------------------------------------------
Private Overloads Sub SetRangeValue( _
ByVal targetControl As AutomationElement, ByVal rangeValue As Double)
    If targetControl Is Nothing Then
        Throw New ArgumentException("Argument cannot be null.")
    End If

    Dim rangeValuePattern As RangeValuePattern = _
    GetRangeValuePattern(targetControl)

    If rangeValuePattern.Current.IsReadOnly Then
        Throw New InvalidOperationException("Control is read-only.")
    End If

    Try
        rangeValuePattern.SetValue(rangeValue)
    Catch exc As ArgumentOutOfRangeException
        ' TO DO: Error handling.
    Catch exc As ArgumentException
        ' TO DO: Error handling.
    End Try

End Sub
///--------------------------------------------------------------------
/// <summary>
/// Obtains a RangeValuePattern control pattern from an 
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A RangeValuePattern object.
/// </returns>
///--------------------------------------------------------------------
private RangeValuePattern GetRangeValuePattern(
    AutomationElement targetControl)
{
    RangeValuePattern rangeValuePattern = null;

    try
    {
        rangeValuePattern =
            targetControl.GetCurrentPattern(
            RangeValuePattern.Pattern)
            as RangeValuePattern;
    }
    // Object doesn't support the 
    // RangeValuePattern control pattern
    catch (InvalidOperationException)
    {
        return null;
    }

    return rangeValuePattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains a RangeValuePattern control pattern from an 
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A RangeValuePattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetRangeValuePattern( _
ByVal targetControl As AutomationElement) As RangeValuePattern
    Dim rangeValuePattern As RangeValuePattern = Nothing

    Try
        rangeValuePattern = DirectCast( _
        targetControl.GetCurrentPattern(rangeValuePattern.Pattern), _
        RangeValuePattern)
    Catch exc As InvalidOperationException
        ' Object doesn't support the 
        ' RangeValuePattern control pattern
        Return Nothing
    End Try

    Return rangeValuePattern

End Function 'GetRangeValuePattern

備註

實際設定的值取決於控制項實作。 控制項可能會將要求的 value 向上或向下四捨五入。 消費者介面自動化用戶端可以檢查 Value 屬性,以判斷實際的更新值。 用戶端也可以在 屬性上註冊屬性變更事件,並檢查 中 AutomationPropertyChangedEventArgs 傳回的舊值和新值。

適用於

另請參閱