vel4p08






Visual Basic 4 in 12 Easy Lessons vel4p08.htm













Visual Basic 4 in 12 Easy Lessons vel4p08.htm
























The Program's Description



The Program's Action



The External Module's Code



The Form Module's Code



Description



Description



Close the Application









Project 8









Modular Programming



Stop and Type: This lesson taught you how to break your programs into separate code components consisting of a form, subroutine procedures, function procedures, and external module files. The various pieces of the application work together to comprise the final program that the user sees when he or she runs the program.



By breaking your program into components, you can build upon past work. You can use subroutine procedures and function procedures that you've written before. You can define files of your own public values that you can use in the same way that you already use Visual Basic's vb constants such as vbWhite. When you reuse general-purpose code that you've written before, you save program development time and make debugging much simpler.



In this lesson, you saw the following:





[lb] What subprograms are all about





[lb] How subroutine procedures differ from function procedures





[lb] When to use an external module file





[lb] Why Visual Basic supports three kinds of variable scope





[lb] How you can pass local values from one procedure to another















The Program's Description



This project introduces an interesting problem with the Visual Basic Working Model. The Visual Basic Working Model that comes with this book allows for a maximum of two modules, either two form modules if your application contains two forms or one form module and one external model.



This project requires one form and one external module. Instead of loading them all at once, follow these steps to see how to add the form and the external module to a project so you'll know how to add external modules to your own projects:





1. Load the PROJECT8.VBP project. Notice that the Project window (viewable by selecting View Project) contains no files.





2. Right-click your mouse over the Project window to display the shortcut menu.





3. Select Add File to display the Add File dialog box.





4. Select the Project8.FRM file. (Your system may not be set up to display the .FRM filename extension but you'll learn from the file's icon that you are looking at a form's file and not a module.)





5. Click the OK command button to add the form file.





6. Right-click over the window once again and select Add File.





7. Select the PROJECT8.BAS file. Again, the icon will represent an external module file. (This is the same icon that appears on the second toolbar button, the Module button.)





8. When you click OK, Visual Basic adds the module file to your project.







Figure P8.1 shows your PROJECT8.VBP Project window. As you can see, the project consists of a form file containing the form, controls, and code pertaining to the form such as all the controls' event procedures. The project also contains a file named PROJECT8.BAS, an external module file designed with this application in mind.



Figure P8.1. Project 8's Project window contains two files.



The purpose of this project's application is to use general-purpose procedures, named constants, an external module, and the controls with which you've worked before to tie together this lesson's material.











The Program's Action



When you load and run PROJECT8.VBP, you'll first see an input box asking for a number from 1 to 100. A default value of 50 is used in case the user wants to press Enter to accept the default. The input box keeps displaying until the user enters a number between 1 and 100, or clicks Cancel or OK to get rid of the input box and accept the default value of 50.



After the user enters a valid number, Figure P8.2 shows the PROJECT8.VBP application's form that appears after the user enters a valid number. (The number entered by the user before Figure P8.2's form appeared was 47.)



Figure P8.2. Project 8's form window.



The program assumes that the user's number is a decimal base 10 number and that the number represents a Fahrenheit temperature reading. If you click the Base 16 option button, you'll see the hexadecimal equivalent to the user's number next to the Converted number: description. Click the Base 8 option button and the octal (base 8) representation of the user's number appears in the Bases frame.



The Temperatures frame changes the user's Fahrenheit temperature to Celsius and back again, depending on the chosen option button.



If the user clicks the Change Number command button, the user will be asked to enter a new number.











The External Module's Code



Listing P8.1 contains the complete code listing for the PROJECT8.BAS external module. As you can see, the module contains public variables as well as a general-purpose function that accepts a Fahrenheit single-precision value and returns the Celsius equivalent. You can add this external module file to any application you write that needs to convert Fahrenheit temperatures to Celsius.











The Form Module's Code



Listing P8.2 contains the complete code listing for the PROJECT8.VBP file that uses the PROJECT8.BAS external module.





Input: Listing P8.1. The external module file contains the application's named public values.





1: Option Explicit

2: ' Define public variables for the entire application

3: ' Number base constants that match Index values

4:

5: Public Base10 As Integer

6: Public Base8 As Integer

7: Public Base16 As Integer

8:

9: ' Temperature named constants that match Index values

10: Public Celsius As Integer

11: Public Farenheit As Integer

12:

13: Function GetCel (ByVal Faren As Integer)

14: ' Assumes that a Fahrenheit temperature

15: ' is passed. Returns that temp as Celsius

16: GetCel = (Faren -32) * (5 / 9)

17: End Function









Description



1. Requires that all variables in the module be defined (or be arguments passed from elsewhere).



2. A remark helps explain the purpose of the (general) procedure.



3. The remark continues.



4. Blank lines help separate parts of code.



5. Declare a public variable, available to the entire project, that will hold the base-10 option button index factor.











5. Instead of coding subscripts, the rest of the program uses named public index values.: 16. Always return a value when writing function procedures.







6. Declare a public variable, available to the entire project, that will hold the base-8 option button index factor.



7. Declare a public variable, available to the entire project, that will hold the base-16 option button index factor.



8. Blank lines help separate parts of code.



9. A remark helps explain the purpose of the subsequent code.



10. Declare a public variable, available to the entire project, that will hold the Celsius option button index factor.



11. Declare a public variable, available to the entire project, that will hold the Fahrenheit's option button index factor.



12. A blank line separates the (general) procedure from the function procedure.



13. The general-purpose function procedure begins, receiving a single argument by value.



14. A remark helps explain the purpose of the function.



15. The remark continues on the next line.



16. Calculate the return value by assigning the converted Fahrenheit temperature to the function name.



17. Terminate the function.





Input: Listing P8.2. The form module's code controls the program flow.





1: Public Sub Form_Load()

2: ' Load the public variables with values as

3: ' soon as the form loads

4: Base10 = 10

5: Base8 = 8

6: Base16 = 16

7: Celsius = 0

8: Farenheit = 1

9:

10: Call GetUserNum ' Stored in module file

11: optbase(Base10).Value = True

12: optTemp(Farenheit).Value = True

13:

14: ' Trigger the event procedures

15: ' for option button frames

16: Call optBase_Click(Base10)

17: Call optTemp_Click(Farenheit)

18: End Sub

19:

20: Sub GetUserNum ()

21: ' A subroutine procedure that gets a

22: ' number from 1 to 100 from the user

23: ' and displays the number on the form

24: Dim UserNum As Variant

25: Do

26: UserNum = InputBox("Enter a number from 1 to 100", "Ask", "50")

27: If (UserNum = "") Then ' Check for Cancel

28: ' Restore previous value

29: UserNum = lblUserNum.Caption

30: Exit Sub

31: End If

32: Loop While (UserNum < 1) Or (UserNum > 100)

33: lblUserNum.Caption = UserNum

34: End Sub

35:

36: Sub optBase_Click (Index As Integer)

37: ' Determines what base the converted

38: ' number displays in the base frame

39: Select Case Index

40: Case BASE10:

41: ' No change

42: lblBaseOut.Caption = lblUserNum.Caption

43: Case BASE16:

44: lblBaseOut.Caption = Hex$(lblUserNum.Caption)

45: Case BASE8:

46: lblBaseOut.Caption = Oct$(lblUserNum.Caption)

47: End Select

48: End Sub

49:

50: Sub optTemp_Click (Index As Integer)

51: ' Determines what temperature appears

52: Select Case Index

53: Case CELSIUS:

54: lblTempOut.Caption = GetCel(Val(lblUserNum.Caption))

55: Case FAHRENHEIT:

56: lblTempOut.Caption = lblUserNum.Caption

57: End Select

58: End Sub

59:

60: Sub cmdChange_Click ()

61: ' Asks the user once again for the number

62: ' and calls appropriate click event

63: ' procedures to update two frames

64: Call GetUserNum

65: optbase(BASE10).Value = True

66: optTemp(FAHRENHEIT).Value = True

67: Call optBase_Click(BASE10)

68: Call optTemp_Click(FAHRENHEIT)

69: End Sub

70:

71: Sub cmdExit_Click ()

72: End

73: End Sub









Description



1. Define the procedure that executes right before the user sees the form.



2. Remarks help explain the procedure's purpose.



3. The remark continues to the next line.



4. Define the base-10 index factor for the correct option button.



5. Define the base-8 index factor for the correct option button.



6. Define the base-16 index factor for the correct option button.



7. Define the Celsius index factor for the correct option button.



8. Define the Fahrenheit index factor for the correct option button.











8. Code can trigger event procedures.







9. A blank line helps separate parts of a program.



10. Call the subroutine procedure that asks the user for a number between 1 and 100.



11. Activate the Base 10 option button. When the form finally appears, the Base 10 option button will be selected.



12. Activate the Fahrenheit option button. When the form finally appears, the Fahrenheit option button will be selected.



13. A blank line helps separate parts of a program.



14. A remark explains subsequent code.



15. The remark continues.



16. Trigger a click event procedure for the option buttons with the Bases frame. This event initializes the frame.



17. Trigger a click event procedure for the option buttons with the Fahrenheit frame. This event initializes the frame.



18. Terminate the subroutine event procedure.



19. A blank line separates the Form_Load() procedure from the subroutine procedure that follows.











19. Always check for the user's Cancel command button selection.







20. Define the subroutine procedure that requests a number from the user.



21. A remark explains subsequent code.



22. The remark continues.



23. The remark continues.



24. Define a variant variable that will capture the result of the input box.



25. Begin a loop that will ask the user for a number.



26. Display an input box and wait for the user's response.



***begin margin notes***

***end margin notes***

27. Don't change the user's previously selected number if the user clicks the Cancel command button.



28. A remark explains subsequent code.



29. Put the number back to its current value.



30. Terminate the subroutine procedure early.



31. Terminate the If that checked for the Cancel command button press.



32. Keep asking until the user enters a valid number within the range required.



33. The user has entered a valid number in the input box, so display the result.



34. Terminate the subroutine procedure.



35. A blank line separates the GetUserNum() procedure from the event procedure that follows.



36. Define the event procedure for the Bases option button control array.



37. A remark explains the purpose of the event procedure.



38. The remark continues on the next line.



39. The Index argument can be one of three values: 10, 16, or 8, as set in the Properties window for the three Index values.



40. If the user clicked the Base 10 option button...



41. A remark describes the code that follows.



42. The label inside the Bases frame matches the user's entered number because both are base 10.



43. If the user clicked the Base 16 option button...



44. Display the user's entered number as an hexadecimal string.



45. If the user clicked the Base 8 option button...



46. Display the user's entered number as an octal string.



47. Terminate the Select Case.



48. Terminate the event procedure.



49. A blank line separates the two event procedures.



50. Define the event procedure for the Temperatures option button control array.



51. A remark explains the purpose of the event procedure.



52. The Index argument can be one of two values: 0 or 1, as set in the Properties window for the two Index values.



53. If the user clicked the Celsius option button...



54. Display the user's entered number as a Celsius temperature.



55. If the user clicked the Fahrenheit option button...



56. The label inside the Temperature's frame matches the user's entered number because both are considered to be in Fahrenheit.



57. Terminate the Select Case statement.



58. Terminate the end procedure.



59. A blank line separates the event procedures.



60. Define the event procedure that executes when the user clicks Change Number.



61. A remark explains the purpose of the event procedure.



62. The remark continues on the next line.



63. The remark continues on the next line.



***begin margin note***

64. Call the subroutine procedure that gets a number from the user. No arguments are required.











64. If the subroutine procedure requires no arguments, do not use parentheses.: ***end margin note***







65. Make the Base 10 Bases option button the default.



66. Make the Fahrenheit 10 Bases option button the default.



67. Trigger the event procedure that clicks the default Base 10 option button.



68. Trigger the event procedure that clicks the default Fahrenheit option button.



69. Terminate the event procedure.



70. A blank line separates the event procedures.



71. Define the event procedure for the Exit command button.



72. End the program when this event triggers.



73. Terminate the event procedure.











Close the Application



You can now exit the application and exit Visual Basic. The next lesson explains how to add disk file access to your applications so that you can store and retrieve long-term data in disk files.


















Wyszukiwarka