Visual Basic List API Routines
SendMessage: Align List Box Contents Using Tabstops
Posted:   Wednesday January 8, 1997
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6, and VB3, 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
List items separated by a tab character.

Interestingly, this was my first foray into the Windows API .. about 15 minutes after starting to develop in VB2 I discovered that the only apparent way to column-align items in a listbox was to use a fixed-width font.  But since this was not how other Windows apps looked, a call to Microsoft Support resulted in a fax with the VB code to set the tabs in both a list and textbox. I was hooked on APIs, and never looked back!
 BAS Module Code

 Form Code
To the form, add the following code:

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

Private Const LB_SETTABSTOPS = &H192

Private Sub Form_Load()

  'set up the tabstops in the list boxes
   ReDim tabstop(0 To 2) As Long
  'assign some values to the tabs for the second third and fourth 
  'column (the first is flush against the listbox edge)
   tabstop(0) = 90
   tabstop(1) = 130
   tabstop(2) = 185
  'clear then set the tabs
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)  
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 3, tabstop(0))

End Sub
Once a set of tabstops are set, they remain even after a List1.Clear has been issued.

The values assigned to the tabstop array may appear somewhat arbitrary. In fact, they are based on a control's measurement system - 'dialog units' - and I have yet to find a suitable algorithm for computing the width of a given font into dialog units that can be successfully used to remove the need for trial and error determination of the values to set.

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 representing the first tabstop to use

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

   List1.AddItem item1 & vbTab & item2 & vbTab & item3 & vbTab & item4

Note: vbTab is a VB4-defined constant; users of previous versions should use instead either chr$(9), or define your own variable for the tab character, i.e. tb$ = chr$(9). Remember too that the word ' Tab ' is a reserved keyword in 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