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.

The Radio Button

One VB control that can be conveniently used for this purpose is the radio button (Figure 3-10). This control has Text and Checked properties. The text in the Text property is displayed next to the button, and is typically used to show the choice. Its Checked property is set to either True or False to indicate whether this option is chosen. A radio button with its Checked property set to True will show a dot bullet in its button; otherwise, the button will be empty.

Figure 3-10
The radio button

How Radio Buttons Work in Group

When the Checked property of one radio button is set to True, the same properties of all other radio buttons in the same group will automatically be set to False. If one button is clicked on, all others in the group will be turned off. For the gender question, two radio buttons can be brought into the group box. One can have the Text property set to Male, and the other to Female. At design time, you can also set a default choice by setting the Checked property of either button to True. Figure 3-9 contains the gender radio buttons within a group box as a part of the account tab.
Notice that all radio buttons on a form constitute a single group. Only one radio button can be selected from a group. To have more than one group of radio buttons in a form, you must place each group in a different container, such as a group box or a tab page.
At run time, when an off button is clicked, it will be turned on and its Checked property will be set to True. Clicking an on button will not result in any change. You can presume the on status in the Click event. As an illustration, assume that the two buttons used for gender are named rdbFemale and rdbMale. The following code will cause a message box to display Female button is clicked or Male button is clicked when you click the button.

Private Sub rdbMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbMale.Click MsgBox("Male button is clicked") End Sub Private Sub rdbFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbFemale.Click MsgBox("Female button is clicked") End Sub

Notice again that clicking a radio button that is already on will not turn it off; therefore, there is no need to check the Checked property of the button in the Click event. An alternative to the click event for the radio button is the CheckedChanged event, which is triggered when the radio button’s Checked property is changed either from True to False or from False to True. If you use the CheckedChanged event in your code for a radio button, you should always test to see if the Checked property is True.
In data-entry operations, usually no immediate response by the computer is needed when the user makes a choice among the radio buttons. So, when the user is ready to save the result, say by clicking a Save button, how does your code determine which button is on? Your code must test the Checked property of the radio button. For example, the following code shows which button is on at the time the button named btnSave is clicked.

Private Sub btnSave_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click If rdbtMale.Checked Then MsgBox("Male button is on.") Else MsgBox("Female button is on.") End If ' Place the code to save data here End Sub  

As explained in Chapter 2, the If statement can have the following structure:

If condition Then Statements to execute if the condition is true Else Statements to execute if the condition is false End If

Recall that the Checked property of a radio button is either True or False. The If statement will test to see whether this value is True for rdbMale. If it is, the computer displays Male button is on. If not, it will display Female button is on. An If block structured this way must be closed with the End If statement. Chapter 5, “Decision,” discusses the If statement in more detail. (Note that the code presumes that one of the radio buttons is checked. To ensure the code works properly, set the Checked property of either radio button to True at design time.)

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