Add Horizontal ScrollBars to a ListBox

The standard VB listboxes don`t support horizontal Scrollbars, only vertical ones. Here's an API work-around to get you one!

' In General Declarations
Private Declare Function SendMessageByNum Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Const LB_SETHORIZONTALEXTENT = &H194

To display the horizontal scroll bar, you then just call

SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, 250, 0

List1.hwnd is replaced with the name of your list box, and 250 is replaced with the width you want the scroll bar to be. If you give a width of 0, then the scrollbar will be disappear. This sub routine will automatically workout the greatest width needed.

Sub lstAddHScroll(lst as ListBox)
    ' depends on the scalewidth
    ' if scalemode is Twips then Divide M by 15 to get Pixels

    For a=0 to lst.listcount-1
        if me.textwidth(lst.list(a))>m then m=me.textwidth(lst.list(a))
    Next

    SendMessageByNum lst.hwnd, LB_SETHORIZONTALEXTENT, m/15, 0
End Sub

You might also like...

Comments

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous