velp08.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 974 行 · 第 1/2 页

HTM
974
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp08.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel17.htm"><LINK REL="Previous" HREF="vel16.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp08.htm</H2><P ALIGN=LEFT><A HREF="vel16.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel17.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E126" >Stop &amp; Type</A><UL><LI><A HREF="#E69E112" >The Program's Description</A><LI><A HREF="#E69E113" >The Program's Action</A><LI><A HREF="#E69E114" >The External Module's Code</A><LI><A HREF="#E69E115" >Descriptions</A><LI><A HREF="#E69E116" >The Form Module's Code</A><LI><A HREF="#E69E117" >Descriptions</A><LI><A HREF="#E69E118" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E24"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 8</B></FONT></CENTER></H1><BR><A NAME="E67E27"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Modular Programming</B></FONT></CENTER></H2><BR><BR><A NAME="E68E126"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop &amp; Type</B></FONT></CENTER></H3><BR><P>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.<BR><P>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 global constants just as Microsoft did by providing you with CONSTANT.TXT. When you reuse general-purpose code that you've written before, you save program development time and make debugging much simpler.<BR><P>In this lesson, you saw the following:<BR><UL><LI>What subprograms are all about<BR><BR><LI>How subroutine procedures differ from function procedures<BR><BR><LI>When to use an external module file<BR><BR><LI>Why Visual Basic supports three kinds of variable scope<BR><BR><LI>How you can pass local values from one procedure to another<BR><BR></UL><BR><A NAME="E69E112"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P8.1 shows the PROJECT8.MAK 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 the CONSTANT.TXT named constant file as well as a file named PROJECT8.BAS, an external module file designed with this application in mind.<BR><P><B> <A HREF="P8vel01.gif">Figure P8.1. Project 8's Project window contains three files.</A></B><BR><P>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.<BR><BR><A NAME="E69E113"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Action</B></FONT></CENTER></H4><BR><P>When you load and run PROJECT8.MAK, 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.<BR><P>After the user enters a valid number, Figure P8.2 shows the PROJECT8.MAK 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.)<BR><P><B> <A HREF="P8vel02.gif">Figure P8.2. Project 8's form window.</A></B><BR><P>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.<BR><P>The Temperatures frame changes the user's Fahrenheit temperature to Celsius and back again, depending on the chosen option button.<BR><P>If the user clicks the Change Number command button, the user will be asked to enter a new number.<BR><BR><A NAME="E69E114"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The External Module's Code</B></FONT></CENTER></H4><BR><P>Listing P8.1 contains the complete code listing for the PROJECT8.BAS external module. As you can see, the module contains global named constants 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.<BR><P><FONT COLOR="#000080"><B>Listing P8.1. The external module file contains the application's named constants.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Option Explicit2:3: ' Define global constants for the application4:5: ' Number base constants that match Index values6: Global Const BASE10 = 107: Global Const BASE8 = 88: Global Const BASE16 = 169:10: ' Temperature constants that match Index values11: Global Const CELSIUS = 012: Global Const FAHRENHEIT = 113:14: Function GetCel (ByVal Faren As Integer)15: ' Assumes that a Fahrenheit temperature16: ' is passed. Returns that temp as Celsius17: GetCel = (Faren + 40) * (5 / 9) - 4018: End Function</FONT></PRE><BR><A NAME="E69E115"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: Requires that all variables in the module be defined (or be arguments passed from elsewhere).<BR><P>2: Blank lines help separate parts of code.<BR><P>3: A remark helps explain the purpose of the (general) procedure.<BR><P>4: Blank lines help separate parts of code.<BR><P>5: A remark that explains how the subsequent named constants relate to Index subscripting properties of the two option button control arrays.<BR><P>6: Define a name for the base option button control array item with 10 for an Index. Through the Property window, these three option buttons have Index property values of 10, 0, and 16.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>6: Instead of coding subscripts, the rest of the program uses named constants.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>7: Define a name for the option button control array item with 8 for an Index.<BR><P>8: Define a name for the option button control array item with 16 for an Index.<BR><P>9: Blank lines help separate parts of code.<BR><P>10: A remark helps explain the purpose of the subsequent code.<BR><P>11: Define a name for the temperature option button control array item with 0 for an Index. Through the Property window, these two option buttons have Index property values of 0 and 1.<BR><P>12: Define a name for the option button control array item with 1 for an Index.<BR><P>13: A blank line separates the (general) procedure from the function procedure.<BR><P>14: The general-purpose function procedure begins, receiving a single argument by value.<BR><P>15: A remark helps explain the purpose of the function.<BR><P>16: The remark continues on the next line.<BR><P>17: Calculate the return value by assigning the converted Fahrenheit temperature to the function name.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>17: Always return a value when writing function procedures.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>18: Terminate the function.<BR><BR><A NAME="E69E116"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Form Module's Code</B></FONT></CENTER></H4><BR><P>Listing P8.2 contains the complete code listing for the PROJECT8.MAK file that uses the PROJECT8.BAS external module.<BR><P><FONT COLOR="#000080"><B>Listing P8.2. The form module's code controls the program flow.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: Call GetUserNum ' Stored in module file3: optbase(BASE10).Value = True4: optTemp(FAHRENHEIT).Value = True5:6: ' Trigger the event procedures7: ' for option button frames8: Call optBase_Click(BASE10)9: Call optTemp_Click(FAHRENHEIT)10: End Sub11:12: Sub GetUserNum ()13: ' A subroutine procedure that gets a14: ' number from 1 to 100 from the user15: ' and displays the number on the form16: Dim UserNum As Variant

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?