Boolean Expressions in Visual Basic

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

Create a new program called check-radio and build the form as shown:

There is one checkBox, chkGo with the text "&Go"

There are 3 radio buttons: radYellow, radBlue, and radWhite with the text as shown.

Write the code as shown below:

'Programmer: Janet Joy
'Illustrate check boxes and radio buttons
Public Class Form1
    Private Sub ChkGo_CheckedChanged(sender As Object, e As EventArgs) Handles ChkGo.CheckedChanged
        'Change color to green if Go is checked, red if not
        If ChkGo.Checked Then
            Me.BackColor = Color.ForestGreen
        Else
            Me.BackColor = Color.Red
        End If
    End Sub

    Private Sub RadYellow_CheckedChanged(sender As Object, e As EventArgs) Handles RadYellow.CheckedChanged
        'RadBYellow has changed; either to checked or not checked
        If RadYellow.Checked Then
            Me.BackColor = Color.Yellow
        End If
    End Sub

    Private Sub RadBlue_CheckedChanged(sender As Object, e As EventArgs) Handles RadBlue.CheckedChanged
        'RadBlue has changed; either to checked or not checked
        If RadBlue.Checked Then
            Me.BackColor = Color.Blue
        End If
    End Sub

    Private Sub RadWhite_CheckedChanged(sender As Object, e As EventArgs) Handles RadWhite.CheckedChanged
         'RadWhite has changed; either to checked or not checked
       If RadWhite.Checked Then
            Me.BackColor = Color.White
        End If
    End Sub
End Class

Experiment:

  • Run the program. Try the hot keys and each of the controls.
  • Notice that only one of the radio buttons can be selected.
  • Add another checkBox. Notice that you can select as many of the check boxes as you want.

NEXT: Boolean functions: isNumeric and isDate