HOWTO: Right Justify/Center Text in Single-Line Text Control

ID: Q111952


The information in this article applies to:


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[ASCII 146]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:
    
          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
     


  4. 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 query words: 3.00 align format textbox


Keywords          : kbcode kbCtrl kbVBp300 kbVBp400 
Version           : 3.00 4.00
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: June 21, 1999