共用方式為


ContentControlBase.Validated Event

Occurs when the content control has been successfully validated.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

Syntax

'Declaration
Event Validated As EventHandler
event EventHandler Validated

Remarks

Handle the Validated event to run code after the content control has been successfully validated.

To validate the content control, handle the Validating event. When you validate a content control, you make sure that the text in the control meets certain conditions. For example, if you have a content control that contains a phone number, you can verify that it contains only the appropriate characters (numbers, parentheses, hyphens).

Examples

The following code example demonstrates event handlers for the Validated and Validating events. After the value of the content control is validated, the event handler for the Validated event displays a message box to the end user.

This example assumes that the document contains a PlainTextContentControl named plainTextContentControl1. To use this code, paste it into the ThisDocument class in your project. For C#, you must also attach the event handlers to the Validated and Validating events of plainTextContentControl1.

This example is for a document-level customization.

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 Security

See Also

Reference

ContentControlBase Interface

Microsoft.Office.Tools.Word Namespace