1. Home
  2. Computing & Technology
  3. Visual Basic
Setting Tabs in a ListBox
Question: How can I align columns in a Listbox?
 Join the Discussion
Add to the FAQ!
Discuss this at the
About.Com Forum
 
 Related Resources
• 
The German site
where the VB 6 solution
was discovered

 

VB 6 doesn't provide a convenient way to align columns in a ListBox. A viewer asks how to align colums so they look like this:

Desired Result

"DAVIDMJOHNSO" posted a message describing how VB.NET provided this kind of capability by simply using one of the capabilities already available in the Format method of the String object. Keep in mind that a fixed space font such as Courier must be used in the ListBox.

      Private Sub Form1_Load( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) _
          Handles MyBase.Load
          Dim strfmt As String = "{0,-10}{1,-20}{2,10}"
          ListBox1.Items.Add( _
              String.Format( _
              strfmt, "Mike", "Paulomiknokvilocldk", "2"))
          ListBox1.Items.Add( _
              String.Format( _
              strfmt, "Albert", "Samuel", "3"))
      End Sub
  

VB.NET Solution

This solution completely satisfies the requirement, but a VB 6 solution was needed. Fortunately, VB 6 also provides this capability once you realize that you have to adopt an object oriented method there too.

Dan Appleman's excellent Programmer's Guide to the Win32 API documents a way to send a Windows message to the ListBox control and reset the tab stops on page 1400. The German site, VB-Tec.de provided the rest of the details. 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!

VB 6 Solution

Explore Visual Basic

More from About.com

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

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

All rights reserved.