⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 velp06.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp06.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel13.htm"><LINK REL="Previous" HREF="vel12.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp06.htm</H2><P ALIGN=LEFT><A HREF="vel12.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="vel13.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="#E68E97" >Stop &amp; Type</A><UL><LI><A HREF="#E69E84" >The Program's Description</A><LI><A HREF="#E69E85" >The Program's Action</A><LI><A HREF="#E69E86" >The Event Procedures</A><LI><A HREF="#E69E87" >Descriptions</A><LI><A HREF="#E69E88" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E18"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 6</B></FONT></CENTER></H1><BR><A NAME="E67E21"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Combining Code and Controls</B></FONT></CENTER></H2><BR><BR><A NAME="E68E97"></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 work with advanced array data as well as how to place option button and check box controls on the form. The arrays simplify your programs by letting you step through data and controls using For loops. The option buttons and check boxes offer your users additional ways to select from choices that your program offers.<BR><P>You can now add control to your programs by putting loops in the code that repeat sections of the program. Computers are excellent tools for repeating calculations as many times as necessary to produce results. Loops also provide ways for you to check user input against good and bad values and repeat the input prompting as many times as necessary for the user to enter data in the required format and range.<BR><P>In this lesson, you saw the following:<BR><UL><LI>How arrays help simplify the definition of multiple variables<BR><BR><LI>How control arrays help work with multiple controls (instead of several event procedures, you need to write only one)<BR><BR><LI>When to use check boxes and when to use option buttons<BR><BR><LI>Where to use framed option button groups to create sets of option buttons from which the user can select<BR><BR></UL><BR><A NAME="E69E84"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P6.1 shows the PROJECT6.MAK application as soon as you load and run the program. The project is simple but uses two sets of framed option buttons and two control arrays of option buttons. The project's controls are as follows:<BR><UL><LI>Two frames, one holding 16 color option buttons and the second holding three beeping options<BR><BR><LI>16 option buttons inside the left frame that select a background color for the form<BR><BR><LI>3 option buttons inside the right frame that determine how many beeps the user wants to hear<BR><BR><LI>An Exit command button to terminate the application<BR><BR></UL><P><B> <A HREF="P6vel01.gif">Figure P6.1. The project's opening screen.</A></B><BR><P>The application is simple <I>because</I> of the control arrays. Control arrays greatly simplify an application in which several similar controls are needed to perform similar functions. Without the control arrays, this extremely introductory project would be burdened by at least <I>twenty</I> event procedures just to perform the same task that it currently performs with only <I>three</I> event procedures!<BR><BR><A NAME="E69E85"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Action</B></FONT></CENTER></H4><BR><P>When you first run the program, the form's background color is set to Black simply because the first option button in the left frame is selected. None of the beeping options is set when the program first executes.<BR><P>You can select one of the beeping options in addition to the color options. Remember that when option buttons appear in separate frames, the option buttons are grouped to allow for one selection from each group. Without the frames, you could select a color <I>or</I> select a beep option button, but not both.<BR><P>Click the second beeping option button and you'll hear two beeps. Select a different color option button and the form's background color changes accordingly.<BR><BR><A NAME="E69E86"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Event Procedures</B></FONT></CENTER></H4><BR><P>Listing P6.1 contains PROJECT6.MAK's event procedures. The Index value sent to the optBeep_Click() and to the optColor_Click() event procedures are used inside each procedure to beep or set an appropriate form color.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The QBColor() function is a built-in Visual Basic that returns a hexadecimal color code so that you don't have to know anything about hexadecimal values. When you use an integer value from 0 to 15 inside QBColor()'s parentheses, Visual Basic function sets a color that matches that number. The colors correspond to the order shown on the form. The Index values for the sixteen color values are numbered from 0 to 15, so the event procedure can use that color code inside QBColor().</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#000080"><B>Listing P6.1. The color changing, beeping, and exit event procedures.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub optCol_Click (Index As Integer)2: ' One of the color option buttons3: ' triggered this event4: frmOpt.BackColor = QBColor(Index)5: End Sub6: Sub optBeep_Click (Index As Integer)7: ' The beeping option buttons have Index8: ' properties that begin with 1 that9: ' correspond to the number of beeps.10: Dim Ctr As Integer11: Dim Delay As Long12: For Ctr = 1 To Index13: Beep14: For Delay = 1 To 8000015: ' Do nothing but waste time...16: Next Delay17: Next Ctr18: End Sub19: Sub cmdExit_Click ()20: End21: End Sub</FONT></PRE><BR><A NAME="E69E87"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The color-changing option buttons make up the control array named optCol, so the name of the Click event procedure is optCol_Click(). No matter <I>which</I> of the 16 color option buttons the user clicks, this event procedure executes.<BR><P>2: A remark helps explain the purpose of the event procedure.<BR><P>3: A remark helps explain the purpose of the event procedure.<BR><P>4: Sets the form's background color property to the QBColor() value of the Index that corresponds to the option button clicked.<BR><P>5: Terminate the event procedure.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>5: Without control arrays, each option button would require its own event procedure.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>6: The beeping option buttons make up the control array named optBeep, so the name of the Click event procedure is optBeep_Click(). No matter <I>which</I> of the 3 beeping option buttons the user clicks, this event procedure executes.<BR><P>7: A remark helps explain the purpose of the event procedure.<BR><P>8: A remark helps explain the purpose of the event procedure.<BR><P>9: A remark helps explain the purpose of the event procedure.<BR><P>10: Define an integer variable used for the loop control variable.<BR><P>11: Define a long integer variable used for slowing down the loop.<BR><P>12: Loops enough times to match the option button's Index value, which, according to the Property window, contains the values from 1 to 3.<BR><P>13: Beep the computer's speaker.<BR><P>14: Begin a huge For loop that slows down the beeps.<BR><P>15: A remark is the only thing inside the innermost nested loop.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>15: A nested loop slows down the computer's beeping speed.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>16: Continue wasting some time by repeating the inner loop.<BR><P>17: Beep again if the beeps have not completed yet.<BR><P>18: Terminate the event procedure.<BR><BR><A NAME="E69E88"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Close the Application</B></FONT></CENTER></H4><BR><P>You can now exit the application and exit Visual Basic.<BR><P>Are you getting tired of beeping and color-changing programs? The simple applications that you've been working with have kept your mind focused on the Visual Basic environment and language. You've now reached the halfway point of this book's education. You have enough tools in your Visual Basic programming repertoire to begin writing more powerful applications, and you'll do just that in the next lesson.<P ALIGN=LEFT><A HREF="vel12.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel13.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>

⌨️ 快捷键说明

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