Visual Basic Text API Routines
SendMessage: Align Text Box Contents Using Tabstops
Posted:   Wednesday January 8, 1997
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6, and VB2, VB3 and VB4-16 with appropriate declarations
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch


VBnet CoolTabs
GetTextExtentPoint32: Right-Align List Box Data
SetWindowLong: Right-Align List Box Data and/or the Scrollbar
SetWindowLong: Right-Align List Contents in a Combo
SendMessage: Align Text Box Contents Using Tabstops

SendMessage: Align List Box Contents Using Tabstops
WM_LBUTTONDOWN: Substitute a Tabbed List for a Combo's Dropdown List
WM_LBUTTONDOWN: Substitute a ListView for a Combo's Dropdown List

Like its sister List box method above, this routine sets tabstops in a Textbox (or RichTextBox).
 BAS Module Code

 Form Code
This demonstrates calling the method from the textbox Load event. For a project with many textboxes having individual tabstop settings, the Load code below could be incorporated into its own routine where the textbox is passed as a parameter.:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _ 
   ByVal wMsg As Long, _ 
   ByVal wParam As Long, _
   lParam As Any) As Long


Sub Form_Load()
   'set up the tabstops 
    ReDim tabstop(0 To 2) As Long
   'assign values to the tabs for the second, third and fourth 
   'column (the initial text is flush against the text box edge)
    tabstop(0) = 90
    tabstop(1) = 130
    tabstop(2) = 185
   'set the tabs
    Call SendMessage(Text1.hwnd, EM_SETTABSTOPS, 3, tabstop(0))

End Sub
Once a set of tabstops are set, they remain set even after clearing a textbox. To clear (reset) the tabstops to the default settings, call SendMessage with EM_SETTABSTOPS passing ByVal 0& as both wParam and lParam.

The values assigned to the tabstop array may appear somewhat arbitrary. In fact, they are based on a control's measurement system - 'dialog units'.  Computing the dialog units for occasional use is not a trivial task, so I have created a tabstop-creating utility to do the work for you.  Check out the VBnet CoolTabs page for more information.

The wParam and lParam members of the SendMessage call as used above mean:
  • wParam: 3  - the total number of tabstops to set.
  • lParam: tabstop(0) - the array of tabstops with the index passed representing the first tabstop to use.

To populate the Textbox, use the tab character between strings when adding items ...

    Text1.Text = item1 & vbTab & item2 & vbTab & item3 & vbTab & item4

vbTab is a 32-bit VB-defined constant; users of VB2 or 3 should use instead either chr$(9), or define your own variable for the tab character, i.e. tb = chr$(9). Remember that the word ' Tab ' is a reserved keyword in all versions of Visual Basic.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter