2. Microsoft Visual Basic 2005 BASICS 2
Objectives
Use nested If statements.
Use radio buttons.
Use the Form Load event.
Use the Select Case statement.
3. Microsoft Visual Basic 2005 BASICS 3
Using Nested If Statements
Nested If statement
An If statement placed inside another If
statement
When using nested If statements
It is important that you properly indent the
code.
6. Microsoft Visual Basic 2005 BASICS 6
Using Radio Buttons
Radio buttons are similar to check boxes
with one important difference.
Radio buttons appear in groups.
Only one button can be selected at a time.
7. Microsoft Visual Basic 2005 BASICS 7
Using Radio Buttons (cont.)
Using radio buttons
Create a GroupBox to group the radio
buttons.
Create the radio buttons in the GroupBox.
Write code to use the radio buttons.
9. Microsoft Visual Basic 2005 BASICS 9
Creating a GroupBox Control
GroupBox control
Container for other controls
Controls that you place inside a
GroupBox are treated as one unit.
Option group
Radio buttons in a GroupBox
GroupBox most important properties
(Name) and Text
12. Microsoft Visual Basic 2005 BASICS 12
Creating Radio Buttons in the
GroupBox
Once the
GroupBox is
created
Add radio
buttons to
the
GroupBox.
13. Microsoft Visual Basic 2005 BASICS 13
Setting Radio Button Name
and Text Properties
Radio buttons need to be named
appropriately.
Use the Name property
The Text property
Specifies the message for the attached
label
14. Microsoft Visual Basic 2005 BASICS 14
Setting Radio Button Name
and Text Properties (cont.)
15. Microsoft Visual Basic 2005 BASICS 15
Adding Code to the Radio
Buttons
Coding radio buttons
Requires that you think in an event-driven
way
Use form-level variables as the scope.
To keep track of the option that has been
selected
17. Microsoft Visual Basic 2005 BASICS 17
Processing the Data
The final step
Calculate the result and create the output.
18. Microsoft Visual Basic 2005 BASICS 18
Using a Form Load Event
Procedure
Set the Checked property to True.
The radio button is selected.
When a form is loaded and opened
A special event called a Load event is
triggered.
You can write an event procedure for the
form’s Load event.
19. Microsoft Visual Basic 2005 BASICS 19
Using Select Case
In a Select Case statement
You specify a variable to test.
You then list a number of cases for which
you want to test.
The first line in a Select Case
statement
Specifies the data involved in the decision
20. Microsoft Visual Basic 2005 BASICS 20
Using Select Case (cont.)
You can use conditional operators in a
Case statement.
21. Microsoft Visual Basic 2005 BASICS 21
Summary
If statements can be nested to make
additional decisions within the code of the
If statement.
It is important to indent the code in a
nested If statement to make the code
readable.
22. Microsoft Visual Basic 2005 BASICS 22
Summary (cont.)
Each If statement within a nested If
statement must end with the End If
statement.
Radio buttons appear in groups. Only
one radio button in the group can be
selected at a time.
Radio buttons are sometimes called
option buttons.
23. Microsoft Visual Basic 2005 BASICS 23
Summary (cont.)
The first step in creating a group of radio
buttons is to create a GroupBox control to
contain the radio buttons. The controls
within a GroupBox are treated as one
unit.
The Text property of a GroupBox control
specifies the text that appears at the top
of the GroupBox.
24. Microsoft Visual Basic 2005 BASICS 24
Summary (cont.)
To associate a radio button with a
GroupBox, click the RadioButton tool only
once and draw the radio button in the
GroupBox. If you double-click to create a
radio button, it will not associate itself with
the GroupBox.
The Text property of a GroupBox control
specifies the text that appears on the label
attached to the radio button.
25. Microsoft Visual Basic 2005 BASICS 25
Summary (cont.)
Coding radio buttons involves using form-
level variables that carry values that
reflect the selected radio button.
A form’s Load event procedure is
executed each time a form is loaded and
opened by the program.
26. Microsoft Visual Basic 2005 BASICS 26
Summary (cont.)
The Select Case statement allows you
to make multi-way selections. The Case
statements in a Select Case can test a
range or use conditional operators.
Conditional operators in a Case
statement must include the Is keyword.
As a default, the Case Else statement
is applied if no other Case is true.