How to Set Tab Stops Within a List Box in Visual Basic

ID Number: Q71067

1.00

WINDOWS

Summary:

Visual Basic does not have any intrinsic function for creating

multicolumn list boxes. To create multicolumn list boxes, you must

call several Windows API function calls to set tab stops within the

list box. The tab stops create the multicolumn effect.

This information applies to Microsoft Visual Basic Programming System

version 1.0 for Windows.

More Information:

Multicolumn list boxes can be created by calling several Windows API

function calls: GetFocus, SendMessage, and SetFocus.

The GetFocus function requires no parameters. This function will

return an integer value that represents the handle to the control. Use

GetFocus to get the handle to the control that currently has focus

upon entry to the event-handler procedure. After you store the handle

to the control that currently has focus, set the focus to the desired

list box.

After you set the focus to the list box, you must send a message to

the window's message queue that will reset the tab stops of the list

box. Using the argument LB_SETTABSTOPS as the second parameter to

SendMessage will set the desired tab stops for the multicolumn list

box based on other arguments to the function. The SendMessage function

requires the following parameters for setting the tab stops:

SendMessage (hWnd%,LB_SETTABSTOPS, wParam%, lparam)

where

wParam% is an integer that specifies the number of tab

stops in the list box.

lParam is a long pointer to the first member of an array

of integers containing the tab stop position in

dialog units. (A dialog unit is a horizontal or

vertical distance. One horizontal dialog unit is

equal to 1/4 of the current dialog base-width unit.

The dialog base units are computed based on the

height and the width of the current system font.

The GetDialogBaseUnits function returns the current

dialog base units in pixels.) The tab stops must

be sorted in increasing order; back tabs are not

allowed.

After setting the tab stops with the SendMessage function, calling

PutFocus with the saved handle will return the focus back to the

control that had focus before the procedure call. PutFocus is the

Alias for the Windows API SetFocus function. The Windows API SetFocus

needs to be redefined using the "Alias" keyword because SetFocus is a

reserved word within Visual Basic.

To create multicolumn list boxes within Visual Basic, create a list

box named List1 on Form1. Declare the following Windows API functions

at the module level or in the Global section of your code as follows:

Declare Function GetFocus Lib "user" () As Integer

Declare Function SendMessage Lib "user" (ByVal hwnd As Integer,

ByVal wMsg As Integer,

ByVal wp As Integer,

lp As Any) As Long

Declare Function PutFocus Lib "user" Alias "SetFocus"

(ByVal hWnd%) As Integer

Note: All Declare statements must each be written on one line.

Also declare the following constants:

Const WM_USER = &H400

Const LB_SETTABSTOPS = WM_USER + 19

Include the following code within a SUB procedure:

Sub Form_Click ()

Static tabs(3) As Integer

hOldWnd% = GetFocus() 'Remember who had the focus.

Form1.Show 'Showing the form avoids "Illegal Function Call" on the

'List1.SetFocus statement below.

list1.SetFocus 'Set the focus to the list box.

lbhWnd% = GetFocus() 'Get the handle to the list box.

'Set up the array of defined tab stops.

tabs(1) = 10

tabs(2) = 50

tabs(3) = 90

'Send a message to the message queue.

retVal& = SendMessage(lbhWnd%, LB_SETTABSTOPS, 3, tabs(1))

'Restore the handle to whoever had it.

R% = PutFocus(hOldWnd%)

'Place some elements into the list box.

list1.AddItem "Name" + Chr$(9) + "Rank" + Chr$(9) + "Serial#"

list1.AddItem "J. Doe" + Chr$(9) + "O-3" + Chr$(9) + "1234"

list1.AddItem "J. Blow" + Chr$(9) + "E-1" + Chr$(9) + "5678"

list1.AddItem "F. Smith" + Chr$(9) + "O-6" + Chr$(9) + "0192"

End Sub

Reference(s):

"Programming Windows: the Microsoft Guide to Writing Applications for

Windows 3," Charles Petzold, Microsoft Press, 1990

"Microsoft Windows Software Development Kit: Reference Volume 1,"

version 3.0

WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit

Additional reference words: 1.00 3.00