1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Frequently Asked Questions

Setting tabstops in a listbox control

Setting tabstops in a listbox control
<URL:https://dotnet.currifex.org/dotnet/faqs/listboxtabstops/en/>
----------------------------------------------------------------------------

Setting tabstops in a listbox control

The Windows User Interface API provides a way to align tab-separated items
of a listbox in columns:

    +---------------------------------------+
    | 1    Hamburger               2.55     |
    | 2    Coca Cola               1.99     |
    |                                       |

'SetListBoxTabStops' takes a reference to the listbox and an array of
integers that contains the columns' start positions.  For more information
about the unit used to interpret the data passed to the function, see the
documentation for 'LB_SETTABSTOPS':

\\\
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal wMsg As Int32, _
    ByVal wParam As IntPtr, _
    ByVal lParam() As Int32 _
) As IntPtr

Private Const LB_SETTABSTOPS As Int32 = &H192

Private Function SetListBoxTabStops( _
    ByVal ListBox As ListBox, _
    ByVal TabStops() As Int32 _
) As Boolean
    Return _
        CBool( _
            SendMessage( _
                ListBox.Handle, _
                LB_SETTABSTOPS, _
                New IntPtr(TabStops.Length), _
                TabStops _
            ).ToInt32() _
        )
End Function
///

Usage:

\\\
Me.ListBox1.Items.AddRange( _
    New String() { _
        "1" & ControlChars.Tab & "Hamburger" & ControlChars.Tab & "2.55", _
        "2" & ControlChars.Tab & "Coca Cola" & ControlChars.Tab & "1.99" _
    } _
)
Debug.Assert(SetListBoxTabStops(Me.ListBox1, New Integer() {0, 20, 100}))
///