420 likes | 505 Views
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
E N D
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 • 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)
BackColor Selection 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
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)
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
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
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
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
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
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
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.
Right Justified Frame Using Const instead of Dimmeans the number cannot be changed later on 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
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
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
Arithmetic Homework Revisited Option Explicit Dim Number1 As Integer Dim Number2 As Integer '---------------------------------------------------- Private Sub optAdd_Click() cmdSymbol.Caption = "+" txtResult.Text = "" End Sub
Arithmetic Homework Revisited Private Sub optMultiplication_Click() cmdSymbol.Caption = "*" txtResult.Text = "" End Sub '---------------------------------------------------- Private Sub optSubtract_Click() cmdSymbol.Caption = "-" txtResult.Text = "" End Sub
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
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
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
Arithmetic Homework Revisited Private Sub cmdClear_Click() txtNumber1.Text = "" txtNumber2.Text = "" txtResult.Text = "" txtNumber1.SetFocus End Sub
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
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
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
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
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
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
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
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