1 / 98

Iteration, FileSystemObject and Multiple Forms

Iteration, FileSystemObject and Multiple Forms. OptionButtons and Frames. OptionButtons. A.k.a. Radio Buttons; Hungarian -- opt Used if one and only one choice must be made The one and only one logic is built into the radio button controls

Download Presentation

Iteration, FileSystemObject and Multiple Forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iteration, FileSystemObject and Multiple Forms OptionButtons and Frames

  2. OptionButtons • A.k.a. Radio Buttons; Hungarian -- opt • 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 button was the one selected (true) • Pp. 513-4 in Schneider

  3. Legal matters

  4. Legal matters (cont.)

  5. Legal matters (the code) Private Sub cmdNext_Click() If optAgree.Value Then 'whatever you want to do if the user agrees 'probably go on to another form End If ‘optAgree.Value = True is a redundant use of boolean If optDisagree.Value Then MsgBox ("Sorry, we cannot proceed if you " & _ “do not agree to these terms.") End If End Sub

  6. 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

  7. FRAMES

  8. 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 they can be treated as a separate set • Often used if there is more than one set of radio buttons • Groups can be assigned names by using the frame’s caption property • Pp. 510-1 in Schneider; hungarian=fra

  9. 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, select the frame then paste it into the Frame

  10. Multiple Choice Example

  11. Multiple Choice Example

  12. Multiple Choice Example

  13. 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

  14. 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

  15. Other uses of Frames • Frames are good for grouping controls • That might be positioned together • That might be “disabled” together • That might be made “invisible” together • Etc.

  16. Positioning the ‘Font Size’ Frame

  17. ‘Font Size’ Frame re-Positioned

  18. 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 Dim means the number cannot be changed later on

  19. SELECT CASE

  20. 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 • Pp. 221-8 in Schneider

  21. State Tax Example

  22. State Tax Example

  23. State Tax Example

  24. State Tax Example Private Sub cmdCalculate_Click() Dim dTax As Double Select Case UCase(txtState.Text) Case Is = "PA" dTax = 0.06 Case Is = "CA" dTax = 0.0875 Case Is = "MA" dTax = 0.0725 Case Else dTax = 0.0 End Select txtTotal.Text = txtSubTotal.Text * (1+dTax) End Sub

  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 (e.g. switch) in other languages is not as flexible

  26. LOOPS

  27. Loops • With an If, a condition determines whether or not a block of statements is executed, but they are executed once • In a loop, a condition determines whether or not a block of statements are executed then the condition is retested and the statements possibly re-executed • Chapter 6 in Schneider

  28. Repetition does not necessarily imply loop • The repeated execution of a set of statements can be driven by • Events • E.g. user clicks button for each iteration • Timer events occur periodically • Code • For loops • While loops

  29. 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 (decremented), 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 • Pp. 276-89 in Schneider

  30. Accumulating Interest txtBalance had the following properties set in design time: Locked (True), MultiLine (True) and Scrollbars (Vertical)

  31. Accumulating Interest Private Sub cmdCalculate_Click() Dim iYear As Integer Dim dBalance As Double Dim dRate As Double dBalance = CDbl(txtInitial.Text) dRate = CDbl(txtRate.Text) / 100 txtBalance.Text = ""

  32. Accumulating Interest For iYear = 1 To CInt(txtYears.Text) dBalance = dBalance * (1 + dRate) txtBalance.Text = txtBalance.Text & iYear & “.” & _ vbTab & Format(dBalance, “Currency”) & vbCrLf Next iYear End Sub Visual Basic constant corresponding to Chr(13) (Carriage Return) and Chr(10) (Line Feed) Note: “Count” is a reserved word.

  33. Step • To change the counter by any number 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

  34. Quarterly Interest

  35. Quarterly Interest Private Sub cmdCalculate_Click() Dim sYear As Single Dim dBalance As Double Dim dRate As Double dBalance = CDbl(txtInitialAmount.Text) dRate = CDbl(txtRate.Text) / 100 / 4 Compounded quarterly

  36. Quarterly Interest txtBalance.Text = "" For sYear = 0.25 To CInt(txtLife.Text) Step 0.25 dBalance = dBalance * (1 + dRate) txtBalance.Text = txtBalance.Text & sYear & _ vbTab & Format(dBalance, “Currency”) & vbCrLf Next sYear End Sub Incrementing by a value other than 1

  37. Paying off a loan

  38. Do While Loop Example Private Sub cmdCalculate_Click() Dim dPrinciple As Double Dim dInterest As Double Dim dMonthlyPayment As Double Dim iMonthCount As Integer dPrinciple = CDbl(txtPrinciple.Text) dInterest = CDbl(txtInterest.Text) dMonthlyPayment = CDbl(txtMonthlyPayment.Text) iMonthCount = 0

  39. Do While Loop Example Do While (dPrinciple > 0) dPrinciple = dPrinciple * (1 + dInterest / _ 100 / 12) - dMonthlyPayment iMonthCount = iMonthCount + 1 Loop lblResult.Caption = lblResult.Caption & _ iMonthCount End Sub

  40. Infinite loop • An infinite loop occurs when the programmer assumed that some condition would eventually terminate the loop and this condition did not arise. • The result is that the loop repeats continually until the operating system or the user stops it. • In VB if you haven’t saved and you get into an infinite loop you might lose your work (but try the Pause/Break button).

  41. Getting out of a loop

  42. Getting out of a loop

  43. Getting out of a loop Do While (dPrinciple > 0) dPrinciple = dPrinciple * (1 + dInterest / _ 100 / 12) - dMonthlyPayment If dPrinciple > CDbl(txtPrinciple.Text) Then MsgBox ("You'll never pay off the loan _ at that rate.") Exit Do End If iMonthCount = iMonthCount + 1 Loop Gets one out of the loop Exit For gets one out of a For loop, Exit Sub gets one out of a subroutine and Exit Function gets one out of a function

  44. FILES

  45. Cookie: A Persistence Example • A cookie (sometimes known as a persistence cookie) is a file placed on a user’s computer by a web server that stores information about the user’s having visited and used a web site. • It might store various custom settings, which hyperlinks have been clicked, and so on. • An object or program is said to have persistence if it stores and recalls data from previous executions. • The data is not stored in the program file but in a separate file.

  46. Object Oriented Files • There is more to a file than just a pointer indicating its location. A file • Has a name and location • May exist or not exist • May be read/write or read only • May be in use by another user • Etc. • The above contribute to the properties and methods of an object oriented file • (Note the course handles file reading and writing differently from the old fashioned way in the book.)

  47. File System Object • The first step in accessing a file in VB is to instantiate a new FileSystemObject • The FileSystemObject contains a whole hierarchy of information about a file, e.g. the drive it’s on, the folder it’s in, etc. • One can gather information on, as well as create, delete and change files and folders

  48. Reference • Strictly speaking the FileSystemObject is not a part of VB but of the Scripting Runtime library, therefore one needs to reference the Scripting Runtime library • A reference is a way to expand VB’s namespace – that is, introduce new “key words” • See View, Object Browser • Go to Project/References • Scroll down and select (check) Microsoft Scripting Runtime

  49. References

  50. References Dialog Box

More Related