Printing Text

Printing text in BASIC is done with the Print statement. Similarly, in Visual Basic, you use the Print method. This method works on the object you wish to print. This object can be an entire form, the Debug object, the Printer object, or a Picture control. We’ve already talked about how the Debug.Print method directs output to the Immediate window (in Chapter 3). The other methods display their output on their respective objects:

  • Form1.Print sends output to Form1’s background.

  • Picture1.Print sends the output to the Picture1 control.

  • Printer.Print sends the output to the printer.

WARNING: Recycled paper versus recycled electrons: When testing your printing programs, remember that most of the methods and properties you will use on the Printer object also can be used with the Picture control. So for most of the examples in this chapter, I use the Picture control to show you how the various methods and properties work. Feel free to try any of these programs out using the Printer object by substituting Printer for Picture1, sending your results directly to your printer. Only your recycler knows the difference.

Printing to and beyond the Edges

The Print method works much as you would expect. Each time you call the Print method, the output will begin at the start of the next line, except if the previous Print method ended with a comma or semicolon. In that case, the print would be continued on the same line. Listing 9.4 contains an example of code that allows printing beyond the right edge, and Figure 9.3 shows the results.

Listing 9.4: Command1_Click Event in Print

Private Sub Command1_Click()
Dim i As Integer
Picture1.Cls
For i = 1 To 100
   Picture1.Print “abcdefg”;
Next i
End Sub

Figure 9.3: Any printed output beyond the right edge of the control is lost.

One limitation of this approach is that, eventually, you will reach the end of the object. If you continue to print to the object, the text will fall off the end of the paper or screen and not be visible. Likewise, if you continue to print on the same line, it will also fall off the edge and be lost. Neither of these conditions will generate an error or a warning. Listing 9.5 contains an example of code that allows printing beyond the bottom edge, and Figure 9.4 shows the results.

Listing 9.5: Command2_Click Event in Print

Private Sub Command2_Click()
Dim i As Integer
Picture1.Cls
For i = 1 To 100
   Picture1.Print “abcdefg”
Next i
End Sub

Figure 9.4: Any printed output beyond the bottom edge of the page is lost

NOTE: Clearing the object: The Cls method is used to clear the contents of a form or of a Picture control and to reposition the cursor to the beginning.

Printing in Columns

Just like in the original BASIC, if you separate values by commas, each value will start at the beginning of the next print zone. Print zones are aligned based on absolute measurements, not characters. This makes it easier to create columns, as long as your data is not wider than a print zone. If it’s wider, you need to be careful to ensure that you are starting the data in the proper print zone. The code shown in Listing 9.6 creates columns with commas. Figure 9.5 shows its output.

Listing 9.6: Command3_Click Event in Print

Private Sub Command3_Click()
Picture1.Cls
Picture1.Print “abc”, “def”, “ghi”, “jkl”, “mno”, “pqr”, _
   “stu”, “vwx”
Picture1.Print “abcdef”, “ghijkl”, “mnopqr”, “stuvwx”
Picture1.Print “abcdefghijkl”, “mnopqrstuvwx”
Picture1.Print “abcdefghijklmnopqrstuvwxyz”, _
   “abcdefghijklmnopqrstuvwxyz”
End Sub

Figure 9.5: When you use commas, columns will line up as long as they are close to the same size.

TIP: Things go better with Tab: One of the functions available for the Print method is the Tab function. The Tab function will position the output cursor at a particular column in the output field. The width of a column is determined by the average width of all of the characters in the current font. This is not the most accurate way to align columns of information, but it is better than relying on the effect of commas and semicolons.

Positioning the Cursor

Another way to control printing alignment is by positioning the cursor. Each object (Form, Picture, and Printer) maintains a cursor that describes the current position on the object. Two properties describe the location of the cursor: CurrentX is the horizontal component, and CurrentY is the vertical component. These properties can be read to find the current location or assigned a value to reset the cursor to a new position. As usual with these objects, all measurements are made in twips. (Recall that one twip is exactly one-twentieth of a printer’s point, and there are 1440 twips to the inch or 567 to the centimeter.)

The code in Listing 9.7 shows how to move around a PictureBox control using the CurrentX and CurrentY properties. I save the values of CurrentX and CurrentY into the string t before I execute the Print method so that these values remain unaffected by the printing process. Figure 9.6 shows the output.

Listing 9.7: Command4_Click Event in Print

Private Sub Command4_Click()
Dim t As String
Picture1.Cls
t = FormatNumber(Picture1.CurrentX, 0) & “,” & _
   FormatNumber(Picture1.CurrentY, 0)
Picture1.Print t
Picture1.CurrentX = 0
Picture1.CurrentY = 2880
t = FormatNumber(Picture1.CurrentX, 0) & “,” & _
   FormatNumber(Picture1.CurrentY, 0)
Picture1.Print t
Picture1.CurrentX = 2880
Picture1.CurrentY = 0
t = FormatNumber(Picture1.CurrentX, 0) & “,” & _
   FormatNumber(Picture1.CurrentY, 0)
Picture1.Print t
Picture1.CurrentX = 2880
Picture1.CurrentY = 2880
t = FormatNumber(Picture1.CurrentX, 0) & “,” & _
   FormatNumber(Picture1.CurrentY, 0)
Picture1.Print t
End Sub

Figure 9.6: You can use the CurrentX and CurrentY properties to control the position of a PictureBox control.

Positioning Text

So far, I haven’t talked about where the cursor is oriented with respect to the text you’re going to print. Maybe it’s obvious, but the cursor is always at the upper-left corner of an imaginary box that surrounds the text. The actual size of the box is determined by the size of the tallest character in the text string, the sum of the widths of each individual character in the text string, and the font used to display the text string.

Because this is a rather complicated process, Visual Basic includes two methods that return the size of the string: TextWidth and TextHeight. Also, if you have embedded carriage returns, these methods will compute the total size of the box based on the size of the longest line and the total number of lines.

The code in Listing 9.8 shows how to display the same string of text in four different quadrants surrounding the center point of the cursor. Figure 9.7 shows the results.

NOTE: Why is there a small space below the text string and behind the text string? This is because the TextWidth method allows for space after the last character in the string, and the TextHeight method includes space above and below the string. This way, you don’t need to worry about where to place the next string.

Listing 9.8: Command5_Click Event in Print

Private Sub Command5_Click()
Dim t As String
t = “Hello”
Picture1.Cls
Picture1.CurrentX = 1440
Picture1.CurrentY = 1440
Picture1.Print t
Picture1.CurrentX = 1440 - Picture1.TextWidth(t)
Picture1.CurrentY = 1440
Picture1.Print t
Picture1.CurrentX = 1440
Picture1.CurrentY = 1440 - Picture1.TextHeight(t)
Picture1.Print t
Picture1.CurrentX = 1440 - Picture1.TextWidth(t)
Picture1.CurrentY = 1440 - Picture1.TextHeight(t)
Picture1.Print t
End Sub

Figure 9.7: You can use the TextHeight and TextWidth methods to control the position of text.

In this example, I begin by printing the first string starting at 1 inch from the top and 1 inch from the left margin (remember, 1440 twips equal 1 inch). Next, I print the second string immediately to the right of the first string. I compute its new X coordinate by subtracting the width of the string.

The third string is printed above the second. Its X coordinate is computed by subtracting the width of the string from the 1440-twips X coordinate. Its Y coordinate is computed by subtracting the height of the string from the 1440-twips Y coordinate. Finally, the last string is printed above the first by subtracting the height of the string from the 1-inch Y coordinate.

Changing Font Characteristics

One of the nice features of Visual Basic is that it has the ability to use multiple fonts in a single object. This means that you can make a block of text bold, italic, or underlined. You can also change the size or even the name of the font you wish to use. This allows you to present information more dramatically by emphasizing certain items.

Doing this in Visual Basic is fairly easy. All you need to do is change the Font object associated with the object you are using. In Listing 9.9, you can see that I copied the same code from the code shown in the previous section and inserted some changes to Picture1’s Font property.

Listing 9.9: Command6_Click Event in Print

Private Sub Command6_Click()
Dim t As String
t = “Hello”
Picture1.Cls
Picture1.CurrentX = 1440
Picture1.CurrentY = 1440
Picture1.Print t
Picture1.Font.Bold = True
Picture1.CurrentX = 1440 - Picture1.TextWidth(t)
Picture1.CurrentY = 1440
Picture1.Print t
Picture1.Font.Italic = True
Picture1.CurrentX = 1440
Picture1.CurrentY = 1440 - Picture1.TextHeight(t)
Picture1.Print t
Picture1.Font.Size = 18
Picture1.CurrentX = 1440 - Picture1.TextWidth(t)
Picture1.CurrentY = 1440 - Picture1.TextHeight(t)
Picture1.Print t
Picture1.Font.Size = 8
Picture1.Font.Bold = False
Picture1.Font.Italic = False
End Sub

As you can see in Figure 9.8, most of these changes did not materially change how the information is aligned. However, in the upper-left corner, where I changed the font size to 18, you can see that the text is pulled slightly away from the other three text blocks. This is because the padding areas above, below, and to the right of the text are greater in an 18-point font than in the standard 8-point font.

Figure 9.8: You can change font characteristics to emphasize text.

© 1998 SYBEX Inc. All rights reserved.