How to set tab stops in a list box by using Visual Basic .NET or Visual Studio 2005

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



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 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 specifies an array of integers, sorted in ascending order, 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.

Steps to build the sample

  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows application. Form1 is created by default.

    Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

    For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

  2. Add the following imports declarative:
    Imports System.Runtime.InteropServices
  3. Paste the following code in the Form1 class:
    <DllImport("user32.dll")> _
    Private Shared Function SendMessage( _
    ByVal hWnd As IntPtr, _
    ByVal wMsg As Int32, _
    ByVal wParam As IntPtr, _
    ByVal lParam As IntPtr) _
    As Int32
    End Function
    Private Const LB_SETTABSTOPS As Int32 = &H192
  4. Add a ListBox control to Form1. Size the list box to approximately 400 pixels wide. This information is listed in the Size setting in the properties.

    NOTE: In Microsoft Visual Basic 6.0, coordinates for forms and controls are expressed in twips. In Visual Basic .NET or in Visual Basic 2005, coordinates are expressed in pixels.
  5. Paste the following code in the Load event of the form:
    'Add a few items to the ListBox control.
    Me.ListBox1.Items.Add("January" & vbTab & "February" & vbTab & "March")
    Me.ListBox1.Items.Add("50" & vbTab & "500" & vbTab & "5000")
    Me.ListBox1.Width = 400
  6. Add a CommandButton control to Form1.
  7. Paste the following code in the Click event of the CommandButton control:
    Dim ListBoxTabs() As Integer = {80, 240}
    Dim result As Integer
    Dim ptr As IntPtr
    Dim pinnedArray As GCHandle

    pinnedArray = GCHandle.Alloc(ListBoxTabs, GCHandleType.Pinned)
    ptr = pinnedArray.AddrOfPinnedObject()
    'Send LB_SETTABSTOPS message to ListBox.
    result = SendMessage(Me.ListBox1.Handle, LB_SETTABSTOPS, _
    New IntPtr(ListBoxTabs.Length), ptr)

    'Refresh the ListBox control.
  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.

ID do Artigo: 318600 - Última Revisão: 11 de mai de 2007 - Revisão: 1