To quickly set tab stops in list boxes, use the SendMessage API call.The units used are called Dialog Base Units, which average out to aboutfour per character. Some experimentation is required to get the settingsjust right, but from then on it’s easy. Here’s an example subroutine thatsets three tab stops in a standard list box:
Declare Function SendMessage Lib "User" _ (ByVal hWnd As Integer, ByVal wMsg As Integer, _ ByVal wParam As Integer, lParam As Any) As Long Global Const WM_USER = &H400 Global Const LB_SETTABSTOPS = (WM_USER + 19) Sub SetTabs (Lst As ListBox) ReDim Tabs(0 To 2) As Integer Dim Rtn& Tabs(0) = 70 Tabs(1) = 120 Tabs(2) = 160 Rtn = SendMessage(Lst.hWnd, LB_SETTABSTOPS, _ 3, Tabs(0)) End Sub
Use the API’s GetDialogBaseUnits function to calculate the numberof units required for a given string. The function’s return value containsa number that is four times the average character width of the system font.Thus, to find the width in dialog base units of a string (A$), use thefollowing code:
Declare Function GetDialogBaseUnits& Lib "User" ()DBU& = GetDialogBaseUnits&() 'Extract low word (character width) W% = DBU& And &HFFFF& 'Calculate width of A$ in dialog base units DBUWidth% = (Len(A$) * W%) 4