42
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons

Flow Control II

  • Upload
    alair

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Flow Control II. Code: Select-Case and For-Next Controls: Frames and OptionButtons. OptionButtons. A.k.a. Radio Buttons Used if one and only one choice must be made The one and only one logic is built into the radio button controls - PowerPoint PPT Presentation

Citation preview

Page 1: Flow Control II

1

Flow Control II

Code: Select-Case and For-NextControls: Frames and OptionButtons

Page 2: Flow Control II

2

OptionButtons

A.k.a. Radio Buttons Used if one and only one choice must be made The one and only one logic is built into the radio

button controls The default method on an OptionButton is

associated with the click event; it tells one that that particular choice was most recently selected

Important property: value: Boolean determines if that buttons was the one selected (true)

Page 3: Flow Control II

3

BackColor Selection

Page 4: Flow Control II

4

BackColor Selection

Page 5: Flow Control II

5

BackColor Selection

Option Explicit

Private Sub optBlue_Click() frmSelectColor.BackColor = vbBlueEnd Sub

Private Sub optGreen_Click() frmSelectColor.BackColor = vbGreenEnd Sub

Private Sub optCyan_Click() frmSelectColor.BackColor = vbCyanEnd Sub

Page 6: Flow Control II

6

BackColor Selection (Extended)

optRed’s value property was set to true during development

but the backcolor is assigned only on a click event

One could have another method (e.g. Form_Load) call optRed_Click whichj will give the same effect as if optRed had been clicked

(Note each optionbutton has its own backcolor property, they remain default gray in this example)

Page 7: Flow Control II

7

BackColor Selection (Extended)

Page 8: Flow Control II

8

Multiple choice

From a group of OptionButtons, one and only one can have a true value

But what if there are a number of categories, and the user must choose one item from each category

We have to let the program know which optionbuttons belong to a group

This is done with frames

Page 9: Flow Control II

9

Frame

Recall a form provides a surface or container for controls

A frame is like a form within a form (a container within a container) It groups controls together, so that

can be treated as a separate set Often used if there is more than one

set of radio buttons

Page 10: Flow Control II

10

Putting OptionButtons in Frames

Make the Frame first, then put the new OptionButton in it; if you move a pre-existing OptionButton into a Frame, it’s not really in the Frame

If you use copy and paste to make additional OptionButtons, be sure that the Frame is highlighted before clicking paste (also at this stage we do not want a control array)

To move a pre-existing OptionButton into a Frame, cut it and paste it into the Frame

Page 11: Flow Control II

11

Multiple Choice Example

Page 12: Flow Control II

12

Multiple Choice Example

Page 13: Flow Control II

13

Multiple Choice Example

Page 14: Flow Control II

14

Multiple Choice Example

Option Explicit

Private Sub opt12_Click() txtText.FontSize = 12End Sub

Private Sub opt24_Click() txtText.FontSize = 24End Sub

Private Sub opt8_Click() txtText.FontSize = 8End Sub

Page 15: Flow Control II

15

Multiple Choice Example

Private Sub optArial_Click() txtText.FontName = "Arial"End Sub

Private Sub optCourier_Click() txtText.FontName = "Courier"End Sub

Private Sub optTimes_Click() txtText.FontName = "Times New Roman"End Sub

Page 16: Flow Control II

16

Select-Case

The Select-Case statement is a fancy version of the If-Then structure used when there are several branches of the algorithm that might be taken and which branch is determined by a single expression

P. 140 in Deitel, Deitel and Nieto

Page 17: Flow Control II

17

Other uses of Frames

Frames are good for grouping controls That might be moved together That might be “disabled” together That might be made “invisible”

together Etc.

Page 18: Flow Control II

18

Right Justified Frame

Page 19: Flow Control II

19

Right Justified Frame

Page 20: Flow Control II

20

Right Justified Frame

Const MARGIN As Integer = 200

Private Sub Form_Resize() fraFontSize.Left = frmFonts.ScaleWidth - _

fraFontSize.Width - MARGINEnd Sub

‘don’t need to move optionbuttons just the ‘frame containing them

Using Const instead of Dimmeans the number cannot be changed later on

Page 21: Flow Control II

21

State Tax Example

Page 22: Flow Control II

22

State Tax Example

Page 23: Flow Control II

23

State Tax Example

Page 24: Flow Control II

24

State Tax Example

Private Sub cmdCalculate_Click() Select Case UCase(txtState.Text) Case Is = "PA" txtTotal.Text = txtSubTotal.Text * 1.06 Case Is = "CA" txtTotal.Text = txtSubTotal.Text * 1.0875 Case Is = "MA" txtTotal.Text = txtSubTotal.Text * 1.0725 Case Else txtTotal.Text = txtSubTotal.Text End SelectEnd Sub Capitalizes a string

Page 25: Flow Control II

25

Select Case Flexibility

In Visual Basic the Select-Case statement allows for cases that are Inequalities (e.g. Case Is < 4) Ranges (e.g. Case 53 To 64) Comma separated values (e.g. Case

Is 75, 99, 302) The corresponding statement in

other languages is not as flexible

Page 26: Flow Control II

26

Arithmetic Homework Revisited

Page 27: Flow Control II

27

Arithmetic Homework Revisited

Option ExplicitDim Number1 As IntegerDim Number2 As Integer

'----------------------------------------------------Private Sub optAdd_Click() cmdSymbol.Caption = "+" txtResult.Text = ""End Sub

Page 28: Flow Control II

28

Arithmetic Homework Revisited

Private Sub optMultiplication_Click() cmdSymbol.Caption = "*" txtResult.Text = ""End Sub

'----------------------------------------------------Private Sub optSubtract_Click() cmdSymbol.Caption = "-" txtResult.Text = ""End Sub

Page 29: Flow Control II

29

Arithmetic Homework Revisited

Private Sub txtNumber1_Validate(Cancel As Boolean)

If IsNumeric(txtNumber1.Text) Then Number1 = CInt(txtNumber1.Text) Else MsgBox ("Please enter a number.") txtNumber1.Text = "" Cancel = True End IfEnd Sub

Page 30: Flow Control II

30

Arithmetic Homework Revisited

Private Sub txtNumber2_Validate(Cancel As Boolean)

If IsNumeric(txtNumber2.Text) Then Number2 = CInt(txtNumber2.Text) Else MsgBox ("Please enter a number.") txtNumber2.Text = "" Cancel = True End IfEnd Sub

Page 31: Flow Control II

31

Private Sub cmdSymbol_Click() Select Case cmdSymbol.Caption Case "+" txtResult.Text = Number1 + Number2 Case "-" txtResult.Text = Number1 - Number2 Case "*" txtResult.Text = Number1 * Number2 Case Else MsgBox ("Something is wrong.") End SelectEnd Sub

Page 32: Flow Control II

32

Arithmetic Homework Revisited

Private Sub cmdClear_Click() txtNumber1.Text = "" txtNumber2.Text = "" txtResult.Text = "" txtNumber1.SetFocusEnd Sub

Page 33: Flow Control II

33

For-Next Loop

One of the structures used for repeating steps

A condition is tested, specifically whether a counter falls within some limit If the condition is true, the statements in the for-

next block are executed, at the “bottom” the counter is incremented, and one returns to the “top” of the loop to test the condition again

If the condition is false, the for-next statements are not executed and the next statement executed is the one following Next

P. 132 in Deitel, Deitel and Nieto

Page 34: Flow Control II

34

Accumulating Interest

Page 35: Flow Control II

35

Accumulating Interest

Private Sub cmdCalculate_Click() Dim Year As Integer Dim Balance As Currency Dim Rate As Double Balance =

CDbl(txtInitialAmount.Text) Rate = CDbl(txtRate.Text) / 100

Page 36: Flow Control II

36

Accumulating Interest

txtBalance.Text = "" For Year = 1 To CInt(txtLife.Text) Balance = Balance * (1 + Rate) txtBalance.Text = txtBalance.Text & Year

& _ vbTab & "$" & Balance & vbCrLf Next YearEnd Sub

Page 37: Flow Control II

37

Step

To change the counter by anything other than 1, use the keyword Step to indicate the amount by which the counter should be incremented

For example, For I=10 to 0 Step –1

begins a loop that counts backwards

Page 38: Flow Control II

38

Quarterly Interest

Page 39: Flow Control II

39

Quarterly Interest

Private Sub cmdCalculate_Click() Dim Year As Single Dim Balance As Currency Dim Rate As Double Balance =

CDbl(txtInitialAmount.Text) Rate = CDbl(txtRate.Text) / 100 / 4

Page 40: Flow Control II

40

Quarterly Interest

txtBalance.Text = "" For Year = 0.25 To CInt(txtLife.Text) Step

0.25 Balance = Balance * (1 + Rate) txtBalance.Text = txtBalance.Text & Year &

_ vbTab & "$" & Balance & vbCrLf Next YearEnd Sub

Page 41: Flow Control II

41

Style Notes

Indent all statements within a method

Indent all statements within an If-Then-Else structure

Indent all statements within a Select-Case structure

Indent all statements within a loop structure

Page 42: Flow Control II

42

Style Notes

Put at least a line of space between two methods

Put a comment line ‘-------------------- above a method

Put a comment describing the method above the method

Names of Const variables are put in all capital letters