Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ContentControlBase.Validating Event (2007 System)

Occurs when the contents of the content control are being validated.

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

public event CancelEventHandler Validating

The Validating event is raised when the control loses focus. Handle the Validating event to determine whether the text in the content control is valid, according to criteria that you choose. 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). If the contents are not valid, you can cancel the event and return the focus to the control by setting the Cancel property of the CancelEventArgs parameter of the event handler to true. The practical effect is that the user cannot leave the control until the text is valid.

To run code after the content control has been successfully validated, handle the Validated event.

For more information about handling events, see Consuming Events.

The following code example demonstrates event handlers for the Validating and Validated events. After the end user changes the text in the content control, the event handler for the Validating event uses a regular expression to verify that the text does not contain integers.

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.

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.");
}

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft