VB6 - Adding a horizontal scroll in ListBox

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:

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « VB6 - Adding a horizontal scroll in ListBox », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).