Boolean Expressions in Visual Basic

The check box and radio buttons have a Boolean property checked that is either true or false.

There are also a few Boolean functions.

Start a program with a text box called txtInput.

Write the code below for the text changed event. (This is the default event, so you can just double click on the text box to open this event.)

Private Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
        Dim s As String = Me.txtInput.Text
        If IsNumeric(s) Then
            Me.Text = "A number"
        Else
            Me.Text = "Not a number"
        End If
    End Sub

Experiment: Run the program. Try some values that are numeric and some that are not.

Another Boolean function is isDate. It returns tru if a string is a valid date.

Replace the code with that shown below:

Private Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
        Dim s As String = Me.txtInput.Text
        If IsDate(s) Then
            Me.Text = "A valid date"
        Else
            Me.Text = "Not a valid date"
        End If
    End Sub 

Experiment: Run the program. Try some values that are numeric and some that are not: 3/12/2017 is valid. 3-22-2018 is also valid.

Experiment: Ask the user to enter the year. If the entry is mumeric, test Febraury 29 to see if it is a date. Tell the user if the year they entered is a leap year.

NEXT: Additional Examples