Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSet ListBox and TextBox tab stops in Visual Basic .NET
DescriptionThis example shows how to set ListBox and TextBox tab stops in Visual Basic .NET.
KeywordsListBox, TextBox, set tabs, tabs, tab stops, set tab stops, Visual Basic .NET, VB.NET
CategoriesControls, Controls
 

This example demonstrates three methods for aligning values in columns.

To set tabs in a ListBox, you need to set the control's UseCustomTabOffsets property to true. Then get the control's CustomTabOffset collection and add your tabs to it.

The SetListBoxTabs function shown in the following code does all of this for you. Simply pass it a ListBox and a list or array of tab values.

 
' Set tab stops inside a ListBox.
Private Sub SetListBoxTabs(ByVal lst As ListBox, ByVal tabs _
    As IList(Of Integer))
    ' Make sure the control will use them.
    lst.UseTabStops = True
    lst.UseCustomTabOffsets = True

    ' Get the control's tab offset collection.
    Dim offsets As ListBox.IntegerCollection = _
        lstCars.CustomTabOffsets

    ' Define the tabs.
    For Each tab As Integer In tabs
        offsets.Add(tab)
    Next tab
End Sub
 
Unfortunately I don't know of a .NET-ish only way to set tab stops inside a TextBox. To do this, use the SendMessage API function to send the TextBox the EM_SETTABSTOPS message.

The SetTextBoxTabs function shown in the following code does this for you. Pass it a ListBox and an array of tab values.

 
<DllImport("user32.dll", SetLastError:=True, _
    CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, _
    ByVal Msg As UInteger, ByVal wParam As Integer, ByVal _
    lParam As Integer()) As IntPtr
End Function

Private Const EM_SETTABSTOPS As Integer = &HCB

' Set tab stops inside a TextBox.
Private Sub SetTextBoxTabs(ByVal txt As TextBox, ByVal tabs _
    As Integer())
    SendMessage(txt.Handle, EM_SETTABSTOPS, tabs.Length, _
        tabs)
End Sub
 
The final method is to make the ListBox or TextBox use a fixed width font such as Courier New. Then format the data so each field has the same length. This method has the advantage that you can format columns to align on the left or right. When you use tabs, columns always align on the left.

Download the example to see a ListBox and two TextBoxes demonstrating these techniques.

 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated