Share via


IValueProvider.SetValue(String) 方法

定義

設定控制項的值。

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

參數

value
String

要設定的值。 提供者會負責將值轉換成適當的資料類型。

例外狀況

如果地區設定特有的資訊以不正確的格式傳遞至控制項,例如日期格式不正確。

如果新的值不能從字串轉換成控制項可辨識的格式。

嘗試對未啟用的控制項進行操作。

範例

下列範例程式碼示範自訂控制項,可將其值設定為長度有限的字串。

/// <summary>
/// Sets the value of the control.
/// </summary>
/// <param name="value">
/// The new value.
/// </param>
void IValueProvider.SetValue(string value)
{
    if (((IValueProvider)this).IsReadOnly)
        throw new InvalidOperationException(
            "Operation cannot be performed.");
    // Arbitrary string length limit.
    if (value.Length > 5)
        throw new ArgumentOutOfRangeException(
            "String is greater than five characters in length.");
    controlValue = value;
}
''' <summary>
''' Sets the value of the control.
''' </summary>
''' <param name="value">
''' The new value.
''' </param>
Private Sub SetValue(ByVal value As String) Implements IValueProvider.SetValue
    If (CType(Me, IValueProvider)).IsReadOnly Then
        Throw New InvalidOperationException("Operation cannot be performed.")
    End If
    ' Arbitrary string length limit.
    If value.Length > 5 Then
        Throw New ArgumentOutOfRangeException("String is greater than five characters in length.")
    End If
    controlValue = value
End Sub

備註

單行編輯控制項支援藉由實作 IValueProvider,以程式設計方式存取其內容。 不過,多行編輯控制項不會實作 IValueProvider;它們是改為藉由實作 ITextProvider來提供其內容的存取。

和 之類的 ListItemTreeItem 控制項必須實 IValueProvider 作任何專案的值是否可編輯,而不論控制項目前的編輯模式為何。 如果子專案可編輯,父控制項也必須實 IValueProvider 作。

可編輯的清單專案。
可編輯的清單項目範例

適用於