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



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • 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

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

  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. Add the following using directive at the top of the Form1.h file:
    using namespace System::Runtime::InteropServices;
  5. 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>
  6. Open Form1 in Design view.
  7. Add a ListBox control to Form1, and then size the added ListBox control to approximately 5 inches wide.
  8. 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.
  9. Open Form1 in Design view.
  10. Add a Button control to Form1.
  11. 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:
    1. Click Project, and then click <ProjectName> Properties.
      Note<ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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
  12. 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

Modification Type:MajorLast Reviewed:1/21/2006
Keywords:kbIJW kbWindowsForms kbListBox kbHOWTOmaster KB816176 kbAudDeveloper