Using DateTimePickerFormat with .CustomFormat = "HH:mm:ss" is perhaps the best out-of-the-box approach, but can be restrictive in terms of interacting with the user interface.
For people coming here to add confirmation to MaskedTextBox , there are some good options.
Type check
If you still need automatic parsing, you can add a ValidatingType and click the TypeValidationCompleted Event like this:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load MaskedTextBox1.Mask = "00:00" MaskedTextBox1.ValidatingType = GetType(DateTime) End Sub Private Sub MaskedTextBox1_TypeValidationCompleted(sender As Object, e As TypeValidationEventArgs) _ Handles MaskedTextBox1.TypeValidationCompleted If Not e.IsValidInput Then Me.validationToolTip.ToolTipTitle = "Invalid Time" Me.validationToolTip.Show("The time you supplied must be a valid time in the format HH:mm", sender) End If End Sub
ValidatingType will use the public static Object Parse(string) method for each type passed to it. You can even pass your own custom classes if they implement this signature
User check:
To provide your own validation, you can click the Validating Event like this:
Private Sub MaskedTextBox1_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) _ Handles MaskedTextBox1.Validating Dim senderAsMask = DirectCast(sender, MaskedTextBox) Dim value = senderAsMask.Text Dim parsable = Date.TryParse(value, New Date) If Not parsable Then e.Cancel = True Me.validationToolTip.ToolTipTitle = "Invalid Time" Me.validationToolTip.Show("The time you supplied must be a valid time in the format HH:mm", sender) End If End Sub
In this case, the verification implementation is reliable, but you can implement any logic that you want to define.
Notes :
If the CausesValidation property CausesValidation set to false , the Validating and Validated events are suppressed.
If the Cancel property of the CancelEventArgs parameter CancelEventArgs set to true in the deletion of the validation event, all events that usually occur after the Validating event are suppressed.
For bonus points, hide the tooltip when the user starts typing again:
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) _ Handles MaskedTextBox1.KeyDown Me.validationToolTip.Hide(sender) End Sub