Export (0) Print
Expand All

MaskedTextBox.TypeValidationCompleted Event

Occurs when MaskedTextBox has finished parsing the current value using the ValidatingType property.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event TypeValidationEventHandler TypeValidationCompleted

The MaskedTextBox control will optionally validate user input against the type defined by its MaskedTextBox.ValidatingType property. When this property is not null, the following series of events occurs:

  1. The validation sequence begins when one of the following occurs:

  2. Any of these events result in a call to the Parse method of the type specified with the ValidatingType property. Parse is responsible for the conversion of the formatted input string to the target type. A successful conversion equates to a successful validation.

  3. After Parse returns, the TypeValidationCompleted event is raised. The event handler for this event is most commonly implemented to perform type or mask validation processing. It receives a TypeValidationEventArgs parameter containing information about the conversion; for example, the IsValidInput member indicates whether the conversion was successful.

  4. After the event handler for the TypeValidationCompleted event returns, the standard validation event, Validating, is raised. A handler can be implemented to perform standard validation, perhaps including canceling the event.

  5. If the event is not canceled in step 3, the standard control validation event Validated is raised.

If the Cancel property is set to true in the TypeValidationCompleted event handler, the event will be canceled and the MaskedTextBox control retains focus, unless the subsequent Validating event sets its version of the CancelEventArgs.Cancel property back to false.

The following code example attempts to parse the user's input as a valid DateTime object. If it fails, the TypeValidationCompleted event handler displays an error message to the user. If the value is a valid DateTime, the code verifies that the date supplied is not prior to today's date. This code example requires that your Windows Forms project contains a MaskedTextBox control named MaskedTextBox1 and a ToolTip control named ToolTip1.


private void Form1_Load(object sender, EventArgs e)
{
    maskedTextBox1.Mask = "00/00/0000";
    maskedTextBox1.ValidatingType = typeof(System.DateTime);
    maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
    maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);

    toolTip1.IsBalloon = true;
}

void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
    if (!e.IsValidInput)
    {
        toolTip1.ToolTipTitle = "Invalid Date";
        toolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", maskedTextBox1, 0, -20, 5000);
    }
    else
    {
        //Now that the type has passed basic type validation, enforce more specific type rules.
        DateTime userDate = (DateTime)e.ReturnValue;
        if (userDate < DateTime.Now)
        {
            toolTip1.ToolTipTitle = "Invalid Date";
            toolTip1.Show("The date in this field must be greater than today's date.", maskedTextBox1, 0, -20, 5000);
            e.Cancel = true;
        }
    }
}

// Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    toolTip1.Hide(maskedTextBox1);
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft