1. Home
  2. Computing & Technology
  3. Visual Basic

Setting Tabs in VB.NET and VB6 Forms Components
With a side trip showing how to call an API!

By Dan Mabbutt, About.com

Setting Tabs in Visual Basic

An About Visual Basic reader asked how to align colums in a ListBox using VB6. Unfortunately, VB6 doesn't provide a convenient way to align columns. But VB.NET does!

VB.NET gives you this capability using the Format method of the String object. Keep in mind that a fixed space font such as Courier must be used in the ListBox.

Dim strfmt As String = _
   "{0,-10}{1,-20}"
ListBox1.Items.Add( _
   String.Format( _
   strfmt, "Mike", "2"))
ListBox1.Items.Add( _
   String.Format( _
   strfmt, "Samuel", "3"))

In this example, the format string strfmt is loaded with pairs of numbers. The first is a zero-based counter for the strings to be formatted (0 is "Mike" and "Samuel", 1 is "2" and "3") and the second is a region that left-justifies the strings if it's negative and right-justifies them if it's positive.

This solution completely satisfies the requirement, but the reader asked for a VB6 solution. VB6 can only do this trick using API calls.

Dan Appleman's foundational Programmer's Guide to the Win32 API (sadly out of print now after ten years, but still widely available) documents a way to send a Windows message to the ListBox control and reset the tab stops on page 1400. To run this solution on your own computer, create a new project with a single ListBox and two CommandButton objects and copy the code below into the code window for your project.

Private Declare Function SendMessage Lib "user32" Alias _
   "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
   Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_SETTABSTOPS = &H192

' The number of tabs
Const OBorder = 3

Dim Tabulator(1 To OBorder) As Long

Private Sub Form_Load()
   ' Individual tab values
   Tabulator(1) = 75
   Tabulator(2) = 150
   Tabulator(3) = 200
' Tabulator(4) = 200
' Tabulator(5) = 250
'   ...

   Call MakeList
End Sub

Private Sub MakeList()
   ' Clear the ListBox
   ' set new tab stops and add values
   List1.Clear

   SendMessage List1.hwnd, _
   LB_SETTABSTOPS, OBorder, Tabulator(1)

   List1.AddItem "Mike" & vbTab & "Paulomiknokvilocldk" & vbTab & "2"
   List1.AddItem "Albert" & vbTab & "Samuel" & vbTab & "3"

End Sub

Private Sub Command1_Click()
   ' Expand the tabs
   Tabulator(1) = Tabulator(1) + 1
   Tabulator(2) = Tabulator(2) + 2
   Tabulator(3) = Tabulator(3) + 3
' Tabulator(4) = Tabulator(4) + 4
' Tabulator(5) = Tabulator(5) + 5
'   ...

Call MakeList
End Sub

Private Sub Command2_Click()
' Contract the tabs
   If Tabulator(1) < 0 Then Exit Sub

   Tabulator(1) = Tabulator(1) - 1
   Tabulator(2) = Tabulator(2) - 2
   Tabulator(3) = Tabulator(3) - 3
' Tabulator(4) = Tabulator(4) - 4
' Tabulator(5) = Tabulator(5) - 5
'   ...
Call MakeList
End Sub

The result is just as satisfactory as the VB.NET solution, but a lot more trouble!

More Visual Basic Quick Tips
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Quick Tips
  5. Setting Tabs in VB.NET and VB6 Forms

©2009 About.com, a part of The New York Times Company.

All rights reserved.