Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.

Check Boxes for Independent Choices

For data with a limited number of items that are mutually exclusive, radio buttons are ideal; however, what if these choices are not mutually exclusive, but rather are independent? For example, a health survey questionnaire may ask the respondent whether he or she regularly has breakfast, lunch, and dinner. The respondent may regularly have all, or skip any of the three meals. In such situation, the check box control will be suitable.
The check box appears similar to the radio button (Figure 3-11). It also has the Text property and the Checked property. The Text property displays text in a manner similar to the radio button. The Checked property has a value of True if the check box is clicked on; and False, otherwise. As noted, however, the check boxes work independently of each other. Any number of them can be set on at the same time, regardless of the status of the other check boxes. When clicked on (checked), its squared box shows a check mark (√). If clicked again, the check mark disappears, suggesting it is turned off. This behavior differs from that of the radio button, which once clicked on will stay on until another button in the same group gets clicked on. Therefore, in the check box’s Click event, you cannot assume that the box is on. Your code must test its Checked property. The following code tests if a check box named chkBreakfast is on.

Private Sub chkBreakFast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBreakFast.Click If chkBreakFast.Checked Then MsgBox("The respondent regularly has breakfast!") Else MsgBox("The respondent does not eat breakfast regularly.") End If End Sub

The If statement checks the value of the Checked property. If it is True, the message immediately below the If statement is displayed; otherwise, the message in the Else block is displayed.

Figure 3-11
Check boxes: icon, appearance, and in action

Theoretically, a check box with a text “Male?” or “Female?” will suffice to ascertain a person’s gender. But such a design choice will not be politically correct albeit efficient. As a software developer or systems designer, you should not only be concerned about efficiency but also other factors such as users’ needs, corporate policy, various systems environmental considerations, and so on.

Difference in the Click Event Between the Check Box and the Radio Button

When you click a check box that is off, it will be turned on and vice versa. In either case, the Click event is raised; therefore, you need to test the Checked property of the check box in the Click event. This differs from the way the radio button responds.

The following code is not useful:
Private Sub rbtMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtMale.Click   If rbtMale.Checked Then MsgBox("Male button is clicked.") Else MsgBox("Female button is clicked.") End If End Sub  
This code is not useful because a radio button can only be turned on when it is clicked; thus, the Else portion of the code will never be executed. Refer to the subsection dealing with the radio button for the correct code.
You should use the CheckedChanged event instead, which is triggered when the control’s Checked property changes. The following code will work (note the difference in event names):
Private Sub rbtMale_CheckedChanged(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles rbtMale.CheckedChanged If rbtMale.Checked Then MsgBox("Male button is clicked.") Else MsgBox("Female button is clicked.") End If End Sub

The CheckState Property

The check box has a CheckState property that the radio button does not. This property interacts with the Checked property. Usually, the CheckState property is not used. Inquisitive readers can gain additional insight by working on 3-9 of the Explore and Discover exercises at the end of this chapter.

Last change: February 13 2016 18:47:28.