vel21.htm

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

HTM
2,347
字号
<P>The Print method easily prints string and numeric constants. To print a string or numeric constant, place the constant to the right of the Print method. The following methods send the numbers 1, 2, and 3 to the printed output:<BR><PRE><FONT COLOR="#000080">printer.Print 1Printer.Print 2Printer.Print 3</FONT></PRE><P>When execution hits these three lines of code, Visual Basic sends 1, 2, and 3 to the Printer object with each number appearing on subsequent lines. Every Print method sends a carriage return and line feed sequence to the printer. A lone Print method on a line by itself such as this one:<BR><BR><PRE><FONT COLOR="#000080">printer.Print</FONT></PRE><P>sends a blank line to the printer.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic always adds a space before all positive numeric values printed on the page. The space is where an invisible plus sign appears.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following Print method sends two lines of text to the Printer object:<BR><PRE><FONT COLOR="#000080">printer.Print &quot;Visual Basic makes writing programs&quot;Printer.Print &quot;for Windows easy.&quot;</FONT></PRE><P>When the Windows Print Manager gets these two lines of output, the following appears on the printer's paper:<BR><PRE><FONT COLOR="#000080">Visual Basic makes writing programsfor Windows easy.</FONT></PRE><BR><A NAME="E69E145"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Printing Variables and Controls</B></FONT></CENTER></H4><BR><P>In addition to constants, the Print method prints the contents of variables and controls. The following initializes a string and integer variable and then prints the contents of the variables on the printer:<BR><PRE><FONT COLOR="#000080">FirstName = &quot;Charley&quot;Age = 24Printer.Print FirstNamePrinter.Print Age</FONT></PRE><P>Here is the output produced by these Print methods:<BR><PRE><FONT COLOR="#000080">Charley 24</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Remember that Visual Basic won't send anything to the Printer object until the code that contains Print executes. You would insert Print methods at appropriate places in the code's procedures where printed output is required. For example, if there is a command button labeled Print Report, that command button's Click procedure would contain Print methods.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E146"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Printing Expressions</B></FONT></CENTER></H4><BR><P>If you could print only individual strings, numeric constants, and variables, Print would be extremely limiting. Of course, Print is not that limited. You can combine constants, variables, and expressions to the right of Print methods to produce more complex printed output. The following Print method prints 31:<BR><BR><PRE><FONT COLOR="#000080">printer.Print 25 + (3 * 2)</FONT></PRE><P>The expression can contain both variables, controls, and constants, like this:<BR><BR><PRE><FONT COLOR="#000080">printer.Print Factor * lblWeight.Caption + 10</FONT></PRE><P>If you want to send special characters to the printer, you can do that by using the Chr$() function. The following expression produces a message that includes embedded quotation marks inside the printed string:<BR><BR><PRE><FONT COLOR="#000080">printer.Print &quot;She said, &quot; &amp; Chr$(34) &amp; &quot;I do.&quot; &amp; Chr$(34)</FONT></PRE><P>When execution reaches the former Print method, this is what the Print Manager sends to the printer:<BR><BR><PRE><FONT COLOR="#000080">She said, &quot;I do.&quot;</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>You wouldn't be able to print the quotation marks without the Chr$() function. Usually, Visual Basic suppresses quotation marks when printing string constants.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E147"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Printing Multiple Values</B></FONT></CENTER></H4><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>print zone</I> occurs every 14 columns on the page.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When you need to print several values on one line, you can do so by separating those values with semicolons and commas. The semicolon forces subsequent values to appear right next to each other in the output. The comma forces values to appear in the next print zone.<BR><P>The following two messages print on different lines:<BR><PRE><FONT COLOR="#000080">printer.Print &quot;The sales were &quot;Printer.Print 4345.67</FONT></PRE><P>By using the semicolon, you can force these values to print next to each other:<BR><BR><PRE><FONT COLOR="#000080">printer.Print &quot;The sales were &quot;; 4345.67</FONT></PRE><P>The semicolon also acts to keep automatic carriage return and line feeds from taking place. The following Print method ends with a trailing semicolon:<BR><BR><PRE><FONT COLOR="#000080">printer.Print &quot;The company name is &quot;;</FONT></PRE><P>The trailing semicolon keeps the printer's print head at the end of the message for subsequent output. Therefore, the subsequent Print statement shown next, no matter how much later in the code the Print appears, would print its output right next to the previous Print's output:<BR><BR><PRE><FONT COLOR="#000080">printer.Print lblComName.Caption ' Complete the line</FONT></PRE><P>The semicolon is nice for printing multiple values of different data types of the same line. The following Print prints all of its data on the same line of output:<BR><BR><PRE><FONT COLOR="#000080">printer.Print &quot;Sales:&quot;; totSales; &quot;Region:&quot;; RegNum</FONT></PRE><P>The comma is still sometimes used to force subsequent values to print in the next print zone. The following Print prints names every 14 spaces on the printed line:<BR><BR><PRE><FONT COLOR="#000080">printer.Print DivName1, DivName2, DivName3, DivName4</FONT></PRE><P>No matter how long or short each division name is, the next division name will print in the next print zone. The previous Print might produce output similar to the following:<BR><BR><PRE><FONT COLOR="#000080">North NorthWest South SouthWest</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>When you print lists of numbers or short strings, the comma allows you to easily align each column.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E148"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Utilize the Fonts</B></FONT></CENTER></H4><BR><P>Most Windows-compatible printers support a variety of fonts. The font-related properties are often useful for printing titles and other special output messages in special font sizes and styles.<BR><P>You can add special effects to your printed text by setting the font modifying properties from Table 21.1. For example, the following code first puts the printer in a boldfaced, italicized, 72-point font (a print size of one full inch), and then prints a message:<BR><PRE><FONT COLOR="#000080">printer.FontBold = TruePrinter.FontItalic = TruePrinter.FontSize = 72Printer.Print &quot;I'm learning Visual Basic!&quot;</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The font properties affect <I>subsequent</I> output. Therefore, if you print several lines of text and then change the font size, the text that you've already printed remains unaffected. Visual Basic prints only the subsequent output with the new font.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E69E149"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Better Spacing with </B><FONT COLOR="#FF8000"><B><I>Spc()</I></B></FONT><B> and </B><FONT COLOR="#FF8000"><B><I>Tab()</I></B></FONT></FONT></CENTER></H4><BR><P>The Print method supports the use of the embedded Spc() and Tab() functions to give you additional control over your program's output. Spc() produces a variable number of spaces in the output as determined by the argument to Spc(). The following Print prints a total of ten spaces between the first name and the last:<BR><BR><PRE><FONT COLOR="#000080">printer.Print FirstName; Spc(10); LastName</FONT></PRE><P>The argument that you send to the embedded Tab() function determines in which column the next printed character appears. In the following Print, the date appears in the 50th column on the page:<BR><BR><PRE><FONT COLOR="#000080">printer.Print Spc(50); DateGenerated</FONT></PRE><P>As these examples show, if you print values before or after the Spc() and Tab() functions, you separate the functions from the surrounding printed values using the semicolon.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Spc() and Tab() give you more control over spacing than the comma and semicolon allow.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 21.2 contains some code that computes and prints two housing pricing and taxation values.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Use Spc() and Tab() to control the printer's output spacing.<BR><P><FONT COLOR="#000080"><B>Listing 21.2. Using </B><B>Spc()</B><B> and </B><B>Tab()</B><B>.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Tax1 = TaxRate * HouseVal12: Tax2 = TaxRate * HouseVal23:4: TotalVal = HouseVal1 + HouseVal25: TotTaxes = TaxRate * TotalVal6:7: Printer.Print &quot;House Value&quot;; Tab(20); &quot;Tax&quot;8: Printer.Print Format$(HouseVal1, &quot;Currency&quot;);9: Printer.Print Tab(20); Format$(Tax1, &quot;Currency&quot;)10: Printer.Print Format$(HouseVal2, &quot;Currency&quot;);11: Printer.Print Tab(20); Format$(Tax2, &quot;Currency&quot;)12:13: Printer.Print ' Prints a blank line14:15: Printer.Print &quot;Total tax:&quot;; Spc(5); Format$(TotTaxes, &quot;Currency&quot;)</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Here is a sample of what you may see on the paper after Listing 21.2 executes:<BR><PRE><FONT COLOR="#000080">House Value Tax$76,578.23 $9,189.39$102,123.67 $12,254.81Total tax: $21,444.20</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The Tab(20) function calls in lines 7, 9, and 11 ensure that the second column that contains the tax information is aligned. Also, notice that the trailing semicolons on lines 8 and 10 allow you to continue the Print methods on subsequent lines without squeezing long Print method values onto the same line.<BR><P>Line 13 prints a blank line. Line 15 uses the Spc() function to insert five spaces between the title and the total amount of tax.<BR><BR><A NAME="E68E162"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Initiating the Print</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The physical printing doesn't begin until all output is released to the Print Manager, or until your application issues an EndDoc method.<BR><P>As you send Print methods to the Print Manager via the Printer object, the Print Manager builds the page or pages of output but doesn't release that output until you issue an EndDoc method. EndDoc tells the Print Manager, &quot;I'm done sending output to you, you can print now.&quot;<BR><P>Without EndDoc, Windows would collect all of an application's output and not print any of the output until the application terminates. If you were to write an application that the user runs throughout the day and that prints invoices as customers make purchases, you would need to issue an EndDoc method at the end of each invoice-printing procedure if you wanted each invoice to print at that time.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 21.3 prints a message on the printer and then signals to the Print Manager that the output is ready to go to paper. Without the EndDoc, the Print Manager would hold the output until the application containing the code terminates.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The EndDoc method tells the Print Manager to release all printed output.<BR><P><FONT COLOR="#000080"><B>Listing 21.3. The </B><B>EndDoc</B><B> tells the Print Manager to release the output.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Printer.Print &quot;Invoice #&quot;; invNum2: Printer.Print &quot;Customer:&quot;; cust(CCnt); Tab(20); &quot;Final Sales&quot;3: Printer.Print &quot;Amount of sale:&quot;; Tab(20); Format$(SaleAmt, &quot;Currency&quot;)4: Printer.Print &quot;Tax:&quot;; Tab(20); Format$(tax, &quot;Currency&quot;)5: Printer.Print6: Printer.Print &quot;Total:&quot;; Tab(20); Format$(TotalSale, &quot;Currency&quot;)7:8: ' Release the job for actual printing

⌨️ 快捷键说明

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