Microsoft KB Archive/318600

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

Article ID: 318600

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q318600





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

IN THIS TASK
SUMMARY
 * Steps to build the sample



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 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.

back to the top

Steps to build the sample
 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

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

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

  Add the following imports declarative: Imports System.Runtime.InteropServices   Paste the following code in the Form1 class:  _ 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  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.  Paste the following code in the Load event of the form: 'Add a few items to the ListBox control. Me.ListBox1.Items.Add(&quot;January&quot; & vbTab & &quot;February&quot; & vbTab & &quot;March&quot;) Me.ListBox1.Items.Add(&quot;50&quot; & vbTab & &quot;500&quot; & vbTab & &quot;5000&quot;) Me.ListBox1.Width = 400  Add a CommandButton control to Form1.</li>  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) pinnedArray.Free

'Refresh the ListBox control. Me.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: kbvs2005swept kbvs2005applies kbhowtomaster KB318600

-

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

© Microsoft Corporation. All rights reserved.