Visual Basic Concepts

Displaying Print Output at a Specific Location

See Also

You can control placement of Print output by specifying the drawing coordinates, using either or both of these techniques:

The Cls Method

All the text and graphics on the object that were created with Print and graphics methods can be deleted with the Cls method. The Cls method also resets the drawing coordinates to the origin (0,0), which is the upper-left corner by default. For example, these statements clear:

Setting Drawing Coordinates

You can set the drawing coordinates of forms and picture boxes directly with the CurrentX and CurrentY properties. For example, these statements reset the drawing coordinates to the upper-left corner for Picture1 and for the current form:

Any new text you print appears on top of any text and graphics already at that location. To erase text selectively, draw a box with the Line method and fill it with the background color. Keep in mind that the drawing coordinates specified by CurrentX and CurrentY usually change location when you use a graphics method.

By default, forms and picture boxes use a coordinate system where each unit corresponds to a twip (1,440 twips equal an inch, and approximately 567 twips equal a centimeter). You may want to change the ScaleMode property of the form, picture box, or Printer object from twips to points, because text height is measured in points. Using the same unit of measure for the text and for the object where you will print the text makes it easier to calculate the position of the text.

For More Information   For more information about twips and drawing coordinates, see "Understanding the Coordinate System" later in this chapter.

The TextHeight and TextWidth Methods

Before using the Print method, you can use the TextHeight and TextWidth methods to determine where to position the CurrentX and CurrentY properties. TextHeight returns the height of a line of text, taking into account the object’s font size and style. The syntax is:

[object.]TextHeight(string)

If the string argument contains embedded carriage-return characters (Chr(13)), then the text corresponds to multiple lines, and TextHeight returns the height of the number of lines of text contained in the string. If there are no embedded carriage returns, TextHeight always returns the height of one line of text.

One way to use the TextHeight method is to set the CurrentY property to a particular line. For example, the following statements set the drawing coordinates to the beginning of the fifth line:

CurrentY = TextHeight("sample") * 4
CurrentX = 0

Assuming there are no carriage returns in the sample text, you would use this syntax to set CurrentY to the nth line:

CurrentY = [object.]TextHeight(string) * (n – 1)

If object is omitted, the method applies to the current form. The object argument can be a form, a picture box, or the Printer object.

The TextWidth method returns the width of a string, taking into account the object’s font size and style. This method is useful because many fonts have proportional-width characters. The TextWidth method helps you determine whether the width of the string is larger than the width of the form, picture box, or Printer object.

For example, the following statements use TextWidth and TextHeight to center the text in a box by positioning CurrentX and CurrentY. The name of the box in this example is MealCard.

CurrentX = (BoxWidth - TextWidth("MealCard")) / 2
CurrentY = (Boxheight - TextHeight("MealCard")) / 2

For More Information   See "TextHeight Method" and "TextWidth Method" in the Language Reference.