Microsoft KB Archive/318601

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

Article ID: 318601

Article Last Modified on 2/15/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition

-



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
 * Steps to Build the sample



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
 Create a new Visual C# Windows application. Form1 is created by default.  Add the following using declarative: using System.Runtime.InteropServices;   Paste the following code in the Form1 class: // Declaration of external function. [DllImport(&quot;user32.dll&quot;)] 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;  Add a ListBox control to Form1 and size it to approximately 5 inches wide.  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(&quot;Jan Sales\tFeb Sales\tMar Sales&quot;); this.listBox1.Items.Add(&quot;50\t500\t5000&quot;);  Add a CommandButton control to Form1.</li>  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; </li> 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.</li></ol>

back to the top

Keywords: kbhowto kbhowtomaster kblistbox kbsample kbctrl KB318601

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.