How to Right Justify Items in List Box w/ Tabs & SendMessage

Last reviewed: June 21, 1995
Article ID: Q110958
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

The sample program below shows how to right justify items in a list box.

MORE INFORMATION

This program calls the SendMessage Windows API function to set a tab stop at every character position in the list box. The program prefixes the appropriate number of tabs to right justify each string in the list box. You need to set the maximum allowed string length in the program.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a large list box (List1) to Form1.

  3. Add the following to the Form Load event code:

       Sub Form_Load ()
    
          Const WM_USER = &H400
          Const LB_SETTABSTOPS = WM_USER + 19
          Const maxlen = 10 ' Maximum expected string length in list box.
          tabchar = Chr$(9) ' ASCII code for a tab
          ReDim a$(maxlen)  ' String array to right justify in list box.
          form1.Show        ' Must Show form in Load event before Print
                            ' will become visible.
    
          ' GetDialogBaseUnits() API function lets you calculate the average
          ' width of characters in the system font.
          bu& = GetDialogBaseUnits()
          hiword = bu& \ (2 ^ 16)    ' 16 pixels high in default system font.
          loword = bu& And &HFFFF&   ' 8 pixels wide in default system font.
          Print "System font width and height, in pixels:   " & loword, hiword
    
          'Assign the array of defined tab stops.
          Static tabs(1 To maxlen) As Integer
          For j = 1 To maxlen  ' Set tabs every 4 dialog units (one character):
             tabs(j) = (loword * j) / 2
             ' On most Windows systems, you need only this: tabs(j) = j * 4
          Next
    
          'Send message to the List1 control through the Windows message queue:
          retVal& = SendMessage(List1.hWnd, LB_SETTABSTOPS, maxlen, tabs(1))
    
          For j = 1 To maxlen
             a$(j) = String$(j, "a") ' Assign an arbitrary character string.
             ' Add the appropriate number of tabstops to right justify:
             tabstring = String$(maxlen + 1 - Len(a$(j)), Chr$(9))
             List1.AddItem tabstring & a$(j)
          Next
    
       End Sub
    
    

  4. Add the following Windows API declarations to the General Declarations section:

       Declare Function GetDialogBaseUnits Lib "User" () As Long
       ' Enter the following Declare statement on one, single line:
       Declare Function SendMessage Lib "user" (ByVal hWnd As Integer,
          ByVal wMsg As Integer, ByVal wp As Integer, lp As Any) As Long
    
    

  5. Start the program, or press the F5 key. All strings are right-justified in the list box. Close the form to end the program.


Additional reference words: 3.00 alignment right-align align
KBCategory: kbprg kbcode
KBSubcategory: APrgOther PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.