vel09.htm

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

HTM
2,065
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel09.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel10.htm"><LINK REL="Previous" HREF="velp04.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel09.htm</H2><P ALIGN=LEFT><A HREF="velp04.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="vel10.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="#E68E70" >What You'll Learn</A><LI><A HREF="#E68E71" >The Need for Remarks</A><LI><A HREF="#E68E72" >Remark-able Code</A><LI><A HREF="#E68E73" >Introduction to Message and Input Boxes</A><LI><A HREF="#E68E74" >The MsgBox Statement</A><LI><A HREF="#E68E75" >The MsgBox() Function</A><LI><A HREF="#E68E76" >The InputBox() Functions</A><LI><A HREF="#E68E77" >Homework</A><UL><LI><A HREF="#E69E64" >General Knowledge</A><LI><A HREF="#E69E65" >Write Code That...</A><LI><A HREF="#E69E66" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E13"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 5, Unit 9</B></FONT></CENTER></H1><BR><A NAME="E67E16"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Remarks and Message Boxes</B></FONT></CENTER></H2><BR><BR><A NAME="E68E70"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>The need for remarks<BR><BR><LI>Remarkable code<BR><BR><LI>Introduction to message and input boxes<BR><BR><LI>The MsgBox statement<BR><BR><LI>The MsgBox() function<BR><BR><LI>The InputBox() functions<BR><BR></UL><P>There's more to Visual Basic programs than forms, visual controls, and code. Often, you'll include messages called <I>remarks</I> within your programs that Visual Basic, Windows, and your computer completely ignore. The remarks aren't for the computer but they are for programmers.<BR><P>At other times, you'll need to display messages to users and get answers from users, and no kind of control or Visual Basic object works better than <I>message boxes</I> and <I>input boxes</I>. This unit teaches you how to display and manage message boxes and input boxes. You'll learn how and when message and input boxes work more effectively than labels and text box controls.<BR><BR><A NAME="E68E71"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Need for Remarks</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Remarks help both you and other programmers who might modify and update your Visual Basic applications in the future. Remarks offer descriptive messages that explain in English (or whatever language you prefer) what's going on in the program's event procedures.<BR><P>It's said that a program is written once and read many times. That saying is true because of the nature of applications. Often, you'll write a program that helps you or your business compute required calculations and keep track of daily transactions. Over time, requirements change. Businesses buy and sell other businesses, the government changes its reporting and taxing requirements, and people's needs change. You should realize that, after you write and implement a program, you'll make modifications to that program later. If you use the program in a business, you'll almost certainly make many modifications to the program to reflect changing conditions.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>Program <I>maintenance</I> refers to the modification of programs over time.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>If you program for someone else, the chances are high that others will modify the programs that you write and that you'll modify programs that other programmers write. Therefore, as you write programs, think about the future maintenance that you and others will make. Write your programs clearly, using ample spacing and indention, and add remarks that explain difficult sections of code.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>A <I>remark</I> is a message that you put inside a program's code. Programmers concerned about maintenance know that ample remarks help clarify code and aid future maintenance. Visual Basic completely ignores any and all remarks because those remarks are for people looking at your program code. Users don't see remarks because users don't see the program's code; rather, users see a program's output.<BR><P>Programmers often add remarks to their programs for the following purposes:<BR><UL><LI>To state the programmer's name and the date that the program was written<BR><BR><LI>To describe in the (general) procedure the overall goal of the program<BR><BR><LI>To describe at the top of all procedures the overall goal of that procedure<BR><BR><LI>To explain tricky or difficult statements so that others who modify the program later can understand the lines of code without having to decipher cryptic code<BR><BR></UL><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Even if you write programs for yourself, and if you are the <I>only</I> one who will modify your programs, you should <I>still</I> add remarks to your program! Weeks or months after you write a program, you'll have forgotten the exact details of the program and remarks that you interspersed throughout the code will simplify your maintenance and will help you find the code that you need to change.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Add remarks <I>as you write your programs.</I> Often, programmers will say to themselves, &quot;I'll finish the program and add remarks later.&quot; Trust me&#151;the remarks don't get added. It's only later, when programmers need to modify the program, that they notice the lack of remarks&#151;and regret it.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Add remarks to your program so that you and others can more quickly grasp the nature of the program and can make modifications to it more easily when needed.<BR><BR><A NAME="E68E72"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Remark-able Code</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic supports several remark formats. Unlike some other programming languages, Visual Basic remarks are easy to add to your code, and their free-form nature enables you to add remarks whenever and wherever needed.<BR><P>Visual Basic supports the following two kinds of remarks:<BR><UL><LI>Remarks that begin with the Rem statement<BR><BR><LI>Remarks that begin with the apostrophe (')<BR><BR></UL><P>The Rem statement is more limiting than the apostrophe and isn't as easy to use as apostrophes. Nevertheless, you'll run across programs that use Rem statements, so you should learn how Rem works. Here is the format of the Rem statement:<BR><BR><PRE><FONT COLOR="#000080">Rem The remark's text</FONT></PRE><P>You can put anything you want in place of <I>The remark's text</I>. Therefore, all of the following are remarks:<BR><PRE><FONT COLOR="#000080">Rem Programmer: Bob Enyart, Date: Mar-27-1996Rem This program supports the check-in and check-outRem process for the dry-cleaning business.Rem This event procedure executes when the userRem clicks on the Exit command button. When pressed,Rem this event procedure closes the program's dataRem files, prints an exception report, and terminatesRem the application</FONT></PRE><P>The first of these remark sections consists of a one-line remark that tells the programmer's name and the date that the program was last modified. If someone else must modify the program later, that person can find the original programmer if needed to ask questions about the program's code. The second remark describes the overall program's goal by stating with a high-level description the program's purpose. The third remark might appear at the top of a command button's Click event procedure.<BR><P>As you can see, you can add one or more lines of remarks depending on the amount of description needed at that point in the program. Visual Basic ignores all lines that begin with Rem. When someone looks at the program code later, that person will know who the programmer is, the date that the program was written, the overall purpose of the program, and the overall description of each procedure that includes a remark section.<BR><P>Say that you used apostrophes in place of the Rem statement in the previous remarks. The following rewritten remarks demonstrate that the remarks are even more effective because Rem doesn't get in the way of the remark's text:<BR><BR><PRE><FONT COLOR="#000080">' Programmer: Bob Enyart, Date: Mar-27-1996' This program supports the check-in and check-out' process for the dry-cleaning business.' This event procedure executes when the user' clicks on the Exit command button. When pressed,' this event procedure closes the program's data' files, prints an exception report, and terminates' the application</FONT></PRE><P>The remarks don't have to go at the beginning of event procedures. You can place remarks between lines of code, as done here:<BR><PRE><FONT COLOR="#000080">Dim Rec As IntegerRem Step through each customer recordFor Rec = 1 To NumCusts ' Test for a high balance If custBal(Rec) &gt; 5000 Then Call PayReq End IfNext Rec</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Don't try to understand the details of this code yet. Concentrate now on the remarks. The code contains some advanced features (Visual Basic <I>arrays</I> and <I>subroutine procedures</I>) that you'll learn about in the last half of this book.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The apostrophe remark offers another advantage over using Rem because you can place apostrophe remarks at the end of Visual Basic statements. By placing a remark to the right of certain lines of code, you can clarify the purpose of the code. Consider how the following code section uses remarks to explain specific lines of code:<BR><BR><PRE><FONT COLOR="#000080">a = 3.14159 * r ^ r ' Calculate a circle's area</FONT></PRE><P>Perhaps only a mathematician could interpret the formula without the remark. The remark helps even nonmathematicians understand the purpose of the statement. There is no reason that you should have to reexamine code every time you look at code. By reading remarks, you can gleam the code's purpose without taking the time to interpret the Visual Basic code.<BR><P>The wrong kind of remarks won't help clarify code, though, so don't overdo remarks. As a matter of fact, lots of lines of code need no remarks to explain their purpose. The following remark is redundant and wastes both your programming time and anyone's who may maintain the program later:<BR><BR><PRE><FONT COLOR="#000080">Dim Sales As Single ' Define a variable named Sales</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 9.1 contains a Select Case routine that you saw in Listing 8.5 of the previous unit. This code contains remarks that help clarify the purpose of the code.

⌨️ 快捷键说明

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