SAMPLE: Setting Tab Stops in a Windows List Box

Last reviewed: February 15, 1996
Article ID: Q66652
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

Tab stops can be used in a list box to align columns of information. This article describes how to set tab stops in a list box and provides a code example that demonstrates the process.

MORE INFORMATION

To set tab stops in a list box, perform the following three steps:

  1. Specify the LBS_USETABSTOPS style when creating the list box.

  2. Assign the desired tab stops to an integer array.

    a. The tab stop values must be in increasing order -- back tab

          stops are not allowed. The tabs work the same as typewriter
          tabs: once a tab stop is overrun, a tab character will move
          the cursor to the next tab stop. If the tab stop list is
          overrun (that is, the current position is greater than the last
          tab stop value), the default tab of eight characters is used.
    

    b. The tab stops should be specified in dialog units. On the

          average, each character is about four horizontal dialog units in
          width.
    

    c. It is possible to hide columns of text from the user by

          specifying tab stops beyond the right side of the list box. This
          can be a useful way to hide information used for the
          application's internal processing.
    

  3. Send an LB_SETTABSTOPS message to the list box to set the tab stops. For example, in Windows 3.1:

          SendMessage(GetDlgItem(hDlg, IDD_LISTBOX),
    
                      LB_SETTABSTOPS,
                      TOTAL_TABS,
                      (LONG)(LPSTR)TabStopList);
    
       a. If wParam is set to 0 (zero) and lParam to NULL, the tab stops are
          set to two dialog units by default.
    
       b. SendMessage() will return TRUE if all of the tab stops are set
          successfully; otherwise, SendMessage() returns FALSE.
    
    

Example

Below is an example of the process. Tab stops are set at character positions 16, 32, 58, and 84.

   int     TabStopList[TOTAL_TABS]; /*  Array to store tabs */

   TabStopList[0] = 16 * 4;          /* 16 spaces */
   TabStopList[1] = 32 * 4;          /* 32 spaces */
   TabStopList[2] = 58 * 4;          /* 58 spaces */
   TabStopList[3] = 84 * 4;          /* 84 spaces */

   SendMessage(GetDlgItem(hDlg, IDD_LISTBOX),
               LB_SETTABSTOPS,
               TOTAL_TABS,
               (LONG)(LPSTR)TabStopList);

NOTE: For Win32, use LPARAM instead of LONG.

If the desired unit of measure is character position, then specifying tab positions in dialog units is recommended. Dialog units are independent of the current font; they are loosely based on the average width of the system font. Each character takes approximately four dialog units.

NOTE: Under Windows 95, dialog base units for dialogs based on non-system fonts are calculated in a different way than under Windows 3.1. For more information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q125681
   TITLE     : How to Calculate Dialog Base Units with Non-system-based
               Font

For more control over the exact placement of a tab stop, the desired position should be converted to a pixel offset and this offset should be converted into dialog units. The following formula will take a pixel position and convert it into the first tab stop position before (or at) the desired pixel position:

   TabStopList[n] = 4 * DesiredPixelPosition /
                     LOWORD(GetDialogBaseUnits());

There is a sample application named TABSTOPS in the Microsoft Software Library that demonstrates how tab stops are set and used in a list box.

Download TABSTOPS.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download TABSTOPS.EXE (size: 22641 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get TABSTOPS.EXE (size: 22641 bytes) 
    


Additional reference words: 3.00 3.10 3.50 4.00 softlib
KBCategory: kbui kbfile
KBSubcategory: UsrCtl


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: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.