Microsoft KB Archive/816176

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

Article ID: 816176

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition

-







For a Microsoft Visual Basic .NET version of this article, see 318600.



For a Microsoft Visual C# .NET version of this article, see 318601.

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

IN THIS TASK

 * SUMMARY
 * Steps to build the sample
 * REFERENCES



SUMMARY
This step-by-step article describes 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 Microsoft Windows Explorer in Details view. You can frequently substitute the ListView control for the ListBox control to get this effect.

To do this, you must send a LB_SETTABSTOPS message to the ListBox control by using the SendMessage function. When you use the LB_SETTABSTOPS message, information that is passed in the wParam parameter and in the lParam parameter of the SendMessage function defines the tab stops. The wParam parameter is an integer that specifies the number of tab stops that are to be set. The lParam parameter specifies an array of integers that are sorted in ascending order, and that define the locations of the tab stops.

The integers 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 four equates to the width of one average character for the specified font; a tab-stop location of eight 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.

This article uses the IJW (It Just Works) mechanism. This avoids using the [DllImportAttribute] directive that is used to declare external functions.

Back to the top

Steps to build the sample
 Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates. By default, Form1 is created.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.  Add the following using directive at the top of the Form1.h file: using namespace System::Runtime::InteropServices;   The SendMessage function is an external function that is declared in the Winuser.h file, and that is included in the Windows.h file. To include the Windows.h file in the Form1.h file, add the following #include statement before the code that you added in the previous step: Open Form1 in Design view.</li> Add a ListBox control to Form1, and then size the added ListBox control to approximately 5 inches wide.</li>  Double-click Form1, and then add the following code to the Load event of Form1: // Add several items to the ListBox control. this->listBox1->Items->Add(S&quot;Jan Sales\tFeb Sales\tMar Sales&quot;); this->listBox1->Items->Add(S&quot;50\t500\t5000&quot;); Note &quot;\t&quot; in this code represents a tab character. </li> Open Form1 in Design view.</li> Add a Button control to Form1.</li>  Double-click the Button control, and then add the following code to the Click event of the Button control: int ListBoxTabs[2] = {80,240}; int result; // Send an LB_SETTABSTOPS message to the ListBox control. result = SendMessage((HWND)this->listBox1->Handle.ToInt32,   LB_SETTABSTOPS, 2, (LPARAM)&ListBoxTabs[0]); // Refresh the ListBox control. this->listBox1->Refresh; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
 * 1) include <windows.h> </li>

To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol> Click Project, and then click <ProjectName> Properties.

Note<ProjectName> is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx </li> Run this sample, and then click the button that you added in step 9. 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

<div class="references_section">