SAMPLE: TabStops.exe Sets Tab Stops in a Windows List Box

ID: Q66652


The information in this article applies to:


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.

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.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ TabStops.exe
For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 : How to Obtain Microsoft Support Files from Online Services


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.

    1. 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.


    2. The tab stops should be specified in dialog units. On the average, each character is about four horizontal dialog units in width.


    3. 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);
     
    1. If wParam is set to 0 (zero) and lParam to NULL, the tab stops are set to two dialog units by default.


    2. 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:
Q125681 : 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()); 

Additional query words: win16sdk


Keywords          : kbfile kbsample kbCtrl kbListBox kbNTOS kbNTOS350 kbNTOS351 kbSDKWin32 kbGrpUser kbWinOS kbWinOS95 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: March 5, 1999