ContentControlBase.Validated 事件 (2007 system)

更新:2007 年 11 月

在成功验证内容控件时发生。

命名空间:  Microsoft.Office.Tools.Word
程序集:  Microsoft.Office.Tools.Word.v9.0(在 Microsoft.Office.Tools.Word.v9.0.dll 中)

语法

声明
Public Event Validated As EventHandler
用法
Dim instance As ContentControlBase
Dim handler As EventHandler

AddHandler instance.Validated, handler
public 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.");
}

权限

另请参见

参考

ContentControlBase 类

ContentControlBase 成员

Microsoft.Office.Tools.Word 命名空间