26
Microsoft Visual Basic 2005 BASICS Lesson 9 Nested If Statements and Radio Buttons

Ppt lesson 09

Embed Size (px)

Citation preview

Page 1: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS

Lesson 9

Nested If Statements and Radio Buttons

Page 2: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 2

Objectives

Use nested If statements. Use radio buttons. Use the Form Load event. Use the Select Case statement.

Page 3: Ppt lesson 09

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.

Page 4: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 4

Using Nested If Statements (cont.)

Page 5: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 5

Using Nested If Statements (cont.)

Page 6: Ppt lesson 09

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.

Page 7: Ppt lesson 09

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.

Page 8: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 8

Using Radio Buttons (cont.)

Page 9: Ppt lesson 09

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

Page 10: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 10

Creating a GroupBox Control (cont.)

Page 11: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 11

Creating a GroupBox Control (cont.)

Page 12: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 12

Creating Radio Buttons in the GroupBox

Once the GroupBox is created Add radio

buttons to the GroupBox.

Page 13: Ppt lesson 09

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

Page 14: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 14

Setting Radio Button Name and Text Properties (cont.)

Page 15: Ppt lesson 09

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

Page 16: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 16

Adding Code to the Radio Buttons (cont.)

Page 17: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 17

Processing the Data

The final step Calculate the result and create the output.

Page 18: Ppt lesson 09

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.

Page 19: Ppt lesson 09

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

Page 20: Ppt lesson 09

Microsoft Visual Basic 2005 BASICS 20

Using Select Case (cont.)

You can use conditional operators in a Case statement.

Page 21: Ppt lesson 09

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.

Page 22: Ppt lesson 09

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.

Page 23: Ppt lesson 09

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.

Page 24: Ppt lesson 09

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.

Page 25: Ppt lesson 09

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.

Page 26: Ppt lesson 09

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.