Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
The MaskedTextBox control will optionally validate user input against the type defined by its MaskedTextBox.ValidatingType property. When this property is not Nothing, the following series of events occurs:
The validation sequence begins when one of the following occurs:
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.
After Parse returns, the 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.
After the event handler for the event returns, the standard validation event, Validating, is raised. A handler can be implemented to perform standard validation, perhaps including canceling the event.
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 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 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 Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MaskedTextBox1.Mask = "00/00/0000" Me.MaskedTextBox1.ValidatingType = GetType(System.DateTime) Me.ToolTip1.IsBalloon = True End Sub Private Sub MaskedTextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted If (Not e.IsValidInput) Then Me.ToolTip1.ToolTipTitle = "Invalid Date" Me.ToolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", Me.MaskedTextBox1, 0, -20, 5000) Else ' Now that the type has passed basic type validation, enforce more specific type rules. Dim UserDate As DateTime = CDate(e.ReturnValue) If (UserDate < DateTime.Now) Then Me.ToolTip1.ToolTipTitle = "Invalid Date" Me.ToolTip1.Show("The date in this field must be greater than today's date.", Me.MaskedTextBox1, 0, -20, 5000) e.Cancel = True End If End If End Sub ' Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires. Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown Me.ToolTip1.Hide(Me.MaskedTextBox1) End Sub
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.