TextWidth Method

See AlsoFI5HLI              Example1MG61WY>Low


Returns the width of a text string as it would be printed in the current font of an object.




The TextWidth method has these parts:

Part                           Description


object                         Object (non MDI form, picture box, or Printer object) that determines the font size upon which the text width is based.

stringexpression         Text string for which the text width is determined.


The width is expressed in terms of the Scale coordinate system of object.  Use TextWidth to determine the amount of horizontal space required to display the text.  If stringexpression contains embedded carriage returns, TextWidth returns the width of the longest line.

See Also

FontSize PropertyB3WV13

Scale MethodQ2ADWT

TextHeight Method4OFKMKO

TextWidth Method Example

The TextWidth method is used to center a line of text horizontally on a form.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim HalfHeight, HalfWidth, Msg              ' Declare variables.

   AutoRedraw = -1                             ' Turn on AutoRedraw.

   BackColor = QBColor(4)                      ' Set background color.

   ForeColor = QBColor(15)                     ' Set foreground color.

   Msg = "Visual Basic"                        ' Create message.

   Fontsize = 48                               ' Set font size.

   HalfWidth = TextWidth(Msg) / 2              ' Calculate half width.

   HalfHeight = TextHeight(Msg) / 2            ' Calculate half height.

   CurrentX = ScaleWidth / 2 - HalfWidth       ' Set X.

   CurrentY = ScaleHeight / 2 - HalfHeight     ' Set Y.

   Print Msg                                   ' Print message.

End Sub