Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Allow Button2 to be clicked without performing validation.
Button2.CausesValidation = False
'Attach Validating event handlers.
AddHandler TextBox1.Validating, AddressOf TextBox1_Validating
AddHandler TextBox2.Validating, AddressOf TextBox2_Validating
AddHandler TextBox3.Validating, AddressOf TextBox3_Validating
End Sub
Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs)
Dim number As Integer
If Not Integer.TryParse(TextBox1.Text, number) OrElse number < 1 OrElse number > 100 Then
TextBox1.SelectAll()
TextBox1.HideSelection = False
MessageBox.Show("TextBox1 must contain a whole number in the range 1 to 100",
"Invalid Data",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
TextBox1.HideSelection = False
e.Cancel = True
End If
End Sub
Private Sub TextBox2_Validating(sender As Object, e As CancelEventArgs)
Dim [date] As Date
If Not Date.TryParse(TextBox2.Text, [date]) OrElse [date] < #1/1/2000# OrElse [date].TimeOfDay <> TimeSpan.Zero Then
TextBox2.SelectAll()
TextBox2.HideSelection = False
MessageBox.Show("TextBox2 must contain a round date no earlier than January 1, 2000",
"Invalid Data",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
TextBox2.HideSelection = False
e.Cancel = True
End If
End Sub
Private Sub TextBox3_Validating(sender As Object, e As CancelEventArgs)
If TextBox3.TextLength = 0 OrElse Not TextBox3.Text.All(Function(ch) Char.IsLetter(ch)) Then
TextBox3.SelectAll()
TextBox3.HideSelection = False
MessageBox.Show("TextBox3 must contain text that consists only of letters",
"Invalid Data",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
TextBox3.HideSelection = False
e.Cancel = True
End If
End Sub
Private Sub TextBoxes_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown,
TextBox2.KeyDown,
TextBox3.KeyDown
Select Case e.KeyData
Case Keys.Enter
'Move forward in the Tab order on Enter.
SelectNextControl(DirectCast(sender, Control), True, True, True, True)
e.SuppressKeyPress = True
Case Keys.Shift Or Keys.Enter
'Move backward in the Tab order on Shift+Enter.
SelectNextControl(DirectCast(sender, Control), False, True, True, True)
e.SuppressKeyPress = True
End Select
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ValidateChildren() Then
'Close the form when all data is valid.
MessageBox.Show("All data is valid. Closing form.")
Close()
Else
MessageBox.Show("Unable to close form with invalid data", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Detach Validating event handlers.
RemoveHandler TextBox1.Validating, AddressOf TextBox1_Validating
RemoveHandler TextBox2.Validating, AddressOf TextBox2_Validating
RemoveHandler TextBox3.Validating, AddressOf TextBox3_Validating
'Close the form whether data is valid or not.
Close()
End Sub
End Class