ContentControlBase.Validated 事件

在成功驗證內容控制項時發生。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Event Validated As EventHandler
event EventHandler Validated

備註

處理 Validated 事件,以便在成功驗證內容控制項之後執行程式碼。

若要驗證內容控制項,請處理 Validating 事件。 驗證內容控制項時,您可以確定控制項中的文字符合某些條件。 例如,如果您有包含電話號碼的內容控制項,就可以驗證它是否只包含適當字元 (數字、括號、連字號)。

如需處理事件的詳細資訊,請參閱使用事件

範例

下列程式碼範例會示範 Validated 和 Validating 事件的事件處理常式。 驗證內容控制項的值之後,Validated 事件的事件處理常式會向使用者顯示訊息方塊。

這個範例會假設文件包含名為 plainTextContentControl1 的 PlainTextContentControl。 若要使用這段程式碼,請將它貼到專案的 ThisDocument 類別中。 若為 C#,您還必須將事件處理常式附加至 plainTextContentControl1 的 Validated 和 Validating 事件。

這是示範文件層級自訂的範例。

Private Sub plainTextContentControl1_Validating(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles PlainTextContentControl1.Validating

    Dim control As Microsoft.Office.Tools.Word.PlainTextContentControl = _
        TryCast(sender, Microsoft.Office.Tools.Word.PlainTextContentControl)

    If control IsNot Nothing Then
        Dim regex As New System.Text.RegularExpressions.Regex("\d")
        If regex.IsMatch(control.Text) Then
            MessageBox.Show("Invalid name. Names cannot contain integers.")
            e.Cancel = True
        End If
    End If
End Sub

Private Sub plainTextContentControl1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles PlainTextContentControl1.Validated

    MessageBox.Show("The name is valid.")
End Sub
void plainTextContentControl1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    Microsoft.Office.Tools.Word.PlainTextContentControl control =
        sender as Microsoft.Office.Tools.Word.PlainTextContentControl;

    if (control != null)
    {
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d");
        if (regex.IsMatch(control.Text))
        {
            MessageBox.Show("Invalid name. Names cannot contain integers.");
            e.Cancel = true;
        }
    }
}

void plainTextContentControl1_Validated(object sender, EventArgs e)
{
    MessageBox.Show("The name is valid.");
}

.NET Framework 安全性

請參閱

參考

ContentControlBase 介面

Microsoft.Office.Tools.Word 命名空間