VB6 - Adding a horizontal scroll in ListBox

Ask a question

Under VB6 the list boxes don't have the horizontal scroll and it may be quite annowing if the length of the texts are longer than the width of the ListBox.
  • This require the expansion of the listBox in proportions that are not necessarily desired for design.
  • These few lines of code generates a horizontal scroll in ListBox.
  • Open a new project.
  • Paste the code below into the module of the form.

Option Explicit  

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 Sub Form_Load()  
Dim Lg As Integer, L As Integer, Va As String, i As Integer  
    'Remplir la list avec des lignes longues pour la démo  
    For i = 0 To 20  
        Va = Va & "Test" & i & " "  
        List1.AddItem Va  
        L = TextWidth(List1.List(i))  
        If Lg < TextWidth(List1.List(i)) Then Lg = L  
    'Adapter les valeurs du scroll horizontal.  
    If Lg > List1.Width Then  
        HorizontalSrcoll List1, Lg  
    End If  
End Sub  

'Adapter les valeurs du scroll horizontal.  
Private Sub HorizontalSrcoll(Lt As Control, Lg As Integer)  
Dim Ret As Long  
Dim ScrollMax As Long  
  ScrollMax = (Lg / Screen.TwipsPerPixelX) + 6  
  Ret = SendMessage(Lt.hwnd, LB_SETHORIZONTALEXTENT, ScrollMax, 0&)  
End Sub

Download project:

VB.Net - A Versatile Timer
VB - Fill a TreeView with the system disks and their directories