How to set tab stops in a list box by using Visual C# .NET (318601)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)

This article was previously published under Q318601
For a Microsoft Visual Basic .NET version of this article, see 318600.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Runtime.InteropServices

IN THIS TASK

SUMMARY

This step-by-step article shows you how to establish custom tab settings in a ListBox control. You may want to do this to display a list box that simulates multiple columns (that is, similar to the appearance of the ListView control in Report view), or to simulate the appearance of Windows Explorer in Details view. Note that you can often substitute the ListView control for the ListBox control to achieve this effect.

To accomplish this task, you must send a LB_SETTABSTOPS message to the ListBox control by using the SendMessage call. When you use the LB_SETTABSTOPS message, information that is passed in the wParam and lParam arguments of the SendMessage call define the tab stops. The wParam argument is an integer that specifies the number of tab stops that are to be set. The lParam argument is an array of integers that define the locations of the tab stops.

The numbers that are supplied as tab-stop locations are measured in units of one quarter of the width of the average character for the font that is selected for the list box. For example, a tab-stop location of 4 equates to the width of one average character for the specified font; a tab-stop location of 8 equates to the width of two characters. However, if the list box is part of a dialog box, the integers are in dialog template units. In addition, the array of tab stops must be sorted in ascending order.

back to the top

Steps to Build the Sample

  1. Create a new Visual C# Windows application. Form1 is created by default.
  2. Add the following using declarative:
    using System.Runtime.InteropServices;
    					
  3. Paste the following code in the Form1 class:
    // Declaration of external function.
    [DllImport("user32.dll")]
    private static extern int SendMessage (int hWnd, int wMsg,
    	int wParam, ref int lParam);
    
    // Windows message constant for setting the tab stops for a list box
    private const int LB_SETTABSTOPS = 0x192;
    					
  4. Add a ListBox control to Form1 and size it to approximately 5 inches wide.
  5. Paste the following code in the Load event of the form:

    NOTE: '\t' in the code below represents a tab character.
    // Add a few items to the ListBox control.
    this.listBox1.Items.Add("Jan Sales\tFeb Sales\tMar Sales");
    this.listBox1.Items.Add("50\t500\t5000");
    					
  6. Add a CommandButton control to Form1.
  7. Paste the following code in the Click event of the CommandButton control:
    // Please note that when building this sample for 64 bit, you will need to change any specfic 32 bit method calls to the appropriate 64 bit call.
    int[] ListBoxTabs = new int[] {80, 240};
    int result;
    
    // Send an LB_SETTABSTOPS message to the ListBox control.
    result = SendMessage(this.listBox1.Handle.ToInt32(),
    	LB_SETTABSTOPS, ListBoxTabs.Length, ref ListBoxTabs[0]);
            
    // Refresh the ListBox control.
    this.listBox1.Refresh();
    					
  8. Run the sample and click the button. Because the values for the tab-stop locations are 80 and 240, the tab stops are set at approximately 20 characters and 60 characters.
back to the top

Modification Type:MajorLast Reviewed:2/15/2006
Keywords:kbCtrl kbhowto kbHOWTOmaster kbListBox kbSample KB318601 kbAudDeveloper