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
topSteps 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:
#include <windows.h>
- Open Form1 in Design view.
- Add a ListBox control to
Form1, and then size the added ListBox control to
approximately 5 inches wide.
- 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"Jan Sales\tFeb Sales\tMar Sales");
this->listBox1->Items->Add(S"50\t500\t5000");
Note "\t" in this code represents a tab character. - Open Form1 in Design view.
- Add a Button control to Form1.
- 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.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note<ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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.
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 - 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.
Back to the
top