vel24.htm

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

HTM
934
字号
<BR><BR><LI>Select Debug Toggle Breakpoint to set a breakpoint. You'll see from the menu command that F9 is the access key for this command. Also, clicking the toolbar's hand icon would also place a breakpoint on this line of code. Figure 24.3 shows how your code window should appear. Visual Basic changes the color of the line to let you know that a breakpoint will take place on that line during the program's execution.<BR>You can turn off a breakpoint by selecting the Debug Toggle Breakpoint (or pressing F9) once again. You can set as many breakpoints as you need throughout a program. Leave the breakpoint in place for now. By setting the breakpoint, you're requesting that Visual Basic halt the program and enter break mode when execution reaches this line of code. Close the Code window and run the program by pressing F5.<BR><BR></OL><P><B><A HREF="24vel03.gif">Figure 24.3. Visual Basic highlights all breakpoints.</A></B><BR><P>As soon as you press F5 to run the program, Visual Basic executes the program. At least, Visual Basic executes the code down to the first breakpoint. As soon as Visual Basic reaches the breakpoint line, Visual Basic enters the break mode <I>before</I> executing the breakpoint line. You can verify the break mode by reading Visual Basic's title bar and seeing the word <B>(break)</B> at the top of the screen.<BR><P>The CHECK2.MAK application is the program that concatenated and assigned a poem into a label's caption. The Form_Load() procedure builds the concatenated poem. The breakpoint that you set occurs in the middle of the poem-building code. Only two values are initialized at the breakpoint. The string variable Newline contains a carriage return and line feed sequence, and the control named lblPoem contains the poem's text.<BR><P>At the breakpoint, the lblPoem label contains the first two lines of the poem. Remember that all controls have default properties, and the Caption property is the default property for the label. Therefore, in building the poem, the code doesn't specify that the poem's text is to enter the Caption property of the label. As you'll see, whenever you specify only the label's name, Visual Basic assumes that you want to assign or display the Caption property.<BR><P>Follow these steps to see what kinds of things you can do at a breakpoint:<BR><OL><LI>By dragging the mouse, highlight either mention of the lblPoem control on the breakpoint's line.<BR><BR><LI>Click the toolbar button with the eyeglass icon. The eyeglass toolbar button produces the Instant Watch dialog box (which is also available from the Debug menu). Visual Basic displays the first line stored in the lblPoem's Caption property, as shown in Figure 24.4.<BR>At first, you may be disappointed that the Instant Watch dialog box doesn't show both lines of the label's Caption property contents. Keep in mind, though, that a label might contain a caption that is several thousand characters long. Visual Basic is able to show you only the characters up to the carriage return and line feed characters at the end of the caption's first line.<BR><BR><LI>Click the Instant Watch dialog box's Cancel command button to close the dialog box. Visual Basic returns to the Code window's breakpoint.<BR><BR><LI>Usually, the programmer will single step through a few lines of code after reaching a breakpoint. To step through the code one line at a time, you can choose Debug Single Step, press F8, or click the single footstep icon on the toolbar. As you single step though the code, Visual Basic highlights the next line of execution. At any point during the single-step process, you can examine variables and controls with the Instant Watch dialog box.<BR><BR></OL><P><B><A HREF="24vel04.gif">Figure 24.4. Displaying the label's value.</A></B><BR><P>Eventually, the Form_Load() procedure ends and the form appears on the screen. If you were to click one of the option buttons, Visual Basic would execute that option button's event procedure and simultaneously open the Code window once again so that you could continue the single step process.<BR><P>Suppose that a variable contains an incorrect value but you're not exactly sure where the error is occurring. You could set a breakpoint at every line of code that changes that variable. When you run the program, you'll look at the contents of that variable before and after each breakpoint's line of execution. If the first breakpoint seems to initialize the variable properly, you don't have to single step through the code until the next breakpoint is reached. Instead of single stepping, you can select Run Continue or press F5 to return the execution to its normal runtime (and real time) mode. When Visual Basic reaches the next breakpoint, the code halts at that next breakpoint and you can continue to examine the variable.<BR><P>Either from the Debug menu or from the Instant Watch dialog box, you can request the Add Watch dialog box. The Add Watch dialog box is shown in Figure 24.5. The Add Watch dialog box allows you to type any expression in the Expression text box.<BR><P><B> <A HREF="24vel05.gif">Figure 24.5. Set up expressions that Visual Basic watches for in the Add Watch </B><B>dialog box.</A></B><BR><P>The Add Watch dialog box is useful for data errors that you can't seem to locate in the code. Rather than break at every line that uses of the data value, you can add a variable or expression to the Add Watch dialog box, and Visual Basic will halt execution when that variable changes, when the expression that you enter in the Add Watch dialog box becomes true, or when Visual Basic changes the value of the expression (the Watch Type frame at the bottom of the Add Watch dialog box determines how you want to set up the watch).<BR><P>For example, suppose that a variable is to maintain a count of customers, but somewhere in your code a negative value appears in the variable. If you added a watch expression such as CustCnt &lt; 0 to the Add Watch dialog box's Expression prompt, and clicked the Break when Expression is True option button, you could then run the program and Visual Basic would enter the break mode at the exact line that caused the variable to become negative.<BR><P>The toolbar buttons with the double footstep icon works almost like the single-step icon except that Visual Basic executes the breakpoint's current procedure in a single-step mode but executes all procedures <I>called</I> by the single-step procedure in their entirety.<BR><P>Terminate your current debug session by selecting Run End. Visual Basic returns to the design mode.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The breakpoints and watch dialog boxes that you can request while debugging your code give you tremendous power in analyzing variables and watching for specific results. You can look at the contents of variables and controls to make sure that data is being initialized the way you expect. Also, the Add Watch dialog box enables you to set up expressions that Visual Basic watches for during the program's execution. If the values of those expressions ever become true or change, Visual Basic will halt the code at that line and allow you to analyze values using the Instant Watch dialog box.<BR><BR><A NAME="E68E183"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Using the Immediate Window</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Immediate window enables you to look at and change variables and controls during the execution of a program.<BR><P>At any breakpoint, you can select Window Debug (Ctrl+B) to request the Debug window. The Debug window is a special window in which you can directly type Visual Basic commands, and view and change variables and control values during a program's execution.<BR><P>For printing, you can apply the Print method (see <A HREF="vel21.htm">Unit 21</A>) to view variables and controls. When you use Print in the Debug window, Visual Basic sends the output to the Debug window and not to the Printer object, as you saw in <A HREF="vel21.htm">Unit 21</A>. For example, suppose that you set a breakpoint during the poem's concatenation, as described in the previous section, and you pressed Ctrl+B to open the Debug window. The Debug window recognizes simple commands and methods such as Print and assignment statements.<BR><P>Figure 24.6 shows what happens if you print the value of the lblPoem. Unlike the Instant Watch dialog box, the Debug window displays the entire multiple line value of the label. You can resize and move the Debug window. Although they must use the Print command instead of simply clicking a variable or control, many users prefer to display values from the Debug window instead of from the Instant Watch dialog box because the Debug window displays the entire value and contains a vertical scroll bar so that they can scroll through the values printed in the window.<BR><P><B> <A HREF="24vel06.gif">Figure 24.6. In the Debug window, you can print values of variables and </B><B>controls.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Use the Debug Window Inside Your Code: The Debug window's scrolling and resizing features are sometimes so handy that some Visual Basic programmers prefer to send messages to the Debug window at runtime rather than use the Instant Watch dialog box. For example, if you want to see the value of certain arguments when called procedures execute, you can add the Print commands at the top of those procedures that send the argument values to the Debug window automatically as the program executes. Once you get the bugs out of the program, you can remove the Print commands so that the Debug window stays closed.<BR>To print to the Debug window, preface the Print method with the Debug object. The following command, executed anywhere from an application's Code window, prints the values of two variables with appropriate titles in the Debug window:<BR>Debug.Print &quot;Age:&quot;; ageVal, &quot;Weight:&quot;; weightVal<BR>All the Print method's options, including semicolons, commas, Tab(), and the Spc() functions, work inside the Debug window just as they did for the Printer object described in <A HREF="vel21.htm">Unit 21</A>. Be careful to specify the Debug object before the Print method, however. If you omit Debug, Visual Basic prints the output directly on the form itself!</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Debug window recognizes assignments that you make to variables and controls. For example, suppose that you know that a certain variable wasn't initialized properly earlier in the execution, but you still want to finish the program's execution. If you need to, you can assign that variable a new value directly within the Debug window using the assignment statement. When you resume the program's execution, either in single-step or in runtime mode, the variable, from that point in the program, will contain the value that you assigned to it.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Debug window gives you more control over the display of variables and controls. The Debug window is resizable and provides you with a scroll bar so that you can see the entire contents of variables and controls as well as keep a running and scrollable tally of those values as you debug the program's execution. The Debug window gives you the ability to assign values to variables and controls as well. In the middle of a program's execution, you can enter the break mode and change the values of anything. When you subsequently resume the program's execution, the program will use those values that you assigned in the Debug window.<BR><BR><A NAME="E68E184"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E167"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What are the two major classifications of errors that a program can contain?<BR><BR><LI>Which error would describe spelling and grammar errors?<BR><BR><LI>What is a <I>debugger</I>?<BR><BR><LI>How can you request that Visual Basic check for syntax errors as you type a program into Visual Basic's Code window?<BR><BR><LI>True or false: Visual Basic displays a message box when a syntax error appears.<BR><BR><LI>True or false: Visual Basic displays a message box when a logic error appears.<BR><BR><LI>Which kind of error, syntax or logic, is the most difficult to find?<BR><BR><LI>What three modes can a program be in?<BR><BR><LI>Which mode is Visual Basic in when you write the program and place items on the form?<BR><BR><LI>How can you tell which mode a program is currently in?<BR><BR><LI>Which mode is perfect for displaying and changing variables?<BR><BR><LI>Name two ways to enter the break mode.<BR><BR><LI>What is a <I>breakpoint</I>?<BR><BR><LI>How can you set a breakpoint?<BR><BR><LI>How do you know that a breakpoint is set when you look through the Code window?<BR><BR><LI>What happens when Visual Basic encounters a breakpoint during the execution of a program?<BR><BR><LI>What is meant by <I>single stepping</I> through a program?<BR><BR><LI>What is the difference between the toolbar button with the single footprint and the double footprint?<BR><BR><LI>What is the difference between the Instant Watch and the Add Watch dialog boxes?<BR><BR><LI>Which kind of watch is most useful for displaying variables at a program's breakpoint?<BR><BR><LI>Which window enables you to view and change Visual Basic variables and controls at runtime?<BR><BR><LI>Which command enables you to display data values in the Debug window?<BR><BR><LI>Which command enables you to assign new values to variables and controls at runtime?<BR><BR></OL><BR><A NAME="E69E168"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Find the Bug</B></FONT></CENTER></H4><BR><P>Jennifer wants to send several values to the Debug window so that she can keep a scrollable list of variable values as she runs a program. Here are some of the statements that Jenny is trying to use to print in the Debug window:<BR>Print lblTitle.Caption<BR>Print x, y, z<BR>Print CompName<BR>Even though Jennifer opens her Debug window during the program's execution, the window is blank. Instead of going to the Debug window, Jennifer's Print methods seem to be appearing on the form itself. Help Jennifer with her problem.<BR><P ALIGN=LEFT><A HREF="vel23.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="velp12.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 + =
减小字号Ctrl + -
显示快捷键?