Download presentation
Presentation is loading. Please wait.
Published byRalph Park Modified over 8 years ago
1
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons
2
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)
3
3 BackColor Selection
4
4
5
5 Option Explicit Private Sub optBlue_Click() frmSelectColor.BackColor = vbBlue End Sub Private Sub optGreen_Click() frmSelectColor.BackColor = vbGreen End Sub Private Sub optCyan_Click() frmSelectColor.BackColor = vbCyan End Sub
6
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)
7
7 BackColor Selection (Extended)
8
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
9
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
10
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
11
11 Multiple Choice Example
12
12 Multiple Choice Example
13
13 Multiple Choice Example
14
14 Multiple Choice Example Option Explicit Private Sub opt12_Click() txtText.FontSize = 12 End Sub Private Sub opt24_Click() txtText.FontSize = 24 End Sub Private Sub opt8_Click() txtText.FontSize = 8 End Sub
15
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
16
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
17
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.
18
18 Right Justified Frame
19
19 Right Justified Frame
20
20 Right Justified Frame Const MARGIN As Integer = 200 Private Sub Form_Resize() fraFontSize.Left = frmFonts.ScaleWidth - _ fraFontSize.Width - MARGIN End Sub ‘don’t need to move optionbuttons just the ‘frame containing them Using Const instead of Dimmeans the number cannot be changed later on
21
21 State Tax Example
22
22 State Tax Example
23
23 State Tax Example
24
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 Select End Sub Capitalizes a string
25
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
26
26 Arithmetic Homework Revisited
27
27 Arithmetic Homework Revisited Option Explicit Dim Number1 As Integer Dim Number2 As Integer '---------------------------------------------------- Private Sub optAdd_Click() cmdSymbol.Caption = "+" txtResult.Text = "" End Sub
28
28 Arithmetic Homework Revisited Private Sub optMultiplication_Click() cmdSymbol.Caption = "*" txtResult.Text = "" End Sub '---------------------------------------------------- Private Sub optSubtract_Click() cmdSymbol.Caption = "-" txtResult.Text = "" End Sub
29
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 If End Sub
30
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 If End Sub
31
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 Select End Sub
32
32 Arithmetic Homework Revisited Private Sub cmdClear_Click() txtNumber1.Text = "" txtNumber2.Text = "" txtResult.Text = "" txtNumber1.SetFocus End Sub
33
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
34
34 Accumulating Interest
35
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
36
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 Year End Sub
37
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
38
38 Quarterly Interest
39
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
40
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 Year End Sub
41
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
42
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.