How to Right Justify/Center Text in Single-Line Text Control

Last reviewed: July 19, 1996
Article ID: Q111952
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, for Windows, version 4.0,

SUMMARY

To center or right justify (align) the text in a text control that contains a single line of text, set the multiline property to True and the Alignment property to the desired value. Then trap the KeyPress event and use the multiline and MaxLength properties to trap the carriage return and convert it to another character.

MORE INFORMATION

Step-by-Step Example to Demonstrate a Right-Justified Text Control

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Add a text control (Text1) to Form1. Set its Alignment property to 1 - Right Justify. Set the Multiline property to True. Set the MaxLength property to some arbitrary value equal to the width of the text box in characters (15). Be sure to make the text control s height property large enough to display the first line of text. The actual height of the text box may need to be a little bigger than normal.

  3. Add the following code to the KeyPress event procedure of Text1:

       '============== Form1.frm ==================
    
       Sub Text1_KeyPress (KeyAscii As Integer)
          If KeyAscii = 13 Then
             KeyAscii = 7            ' Beep - no effect on text
          End If
       End Sub
    
    
Add the following code to the Load event procedure of Form1
   Sub Form_Load()
      Dim boxht as Double
      boxht = Text1.Height
      Text1.Height = Text1.Height * 2
      'This is to make sure the text will appear in the box
      Text1.Height = boxht
   End Sub

  • From the Run menu, choose Start (ALT, R, S) to run the program.

    Type text into the text control, and press the ENTER key. The keystroke is trapped, and the text does not change.


  • Additional reference words: 3.00 align format
    KBCategory: kbprg kbcode
    KBSubcategory: PrgCtrlsStd


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: July 19, 1996
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.