VB6 - Adding a horizontal scroll in ListBox

December 2016


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 Const LB_SETHORIZONTALEXTENT = &H194  
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  
    Next  
    '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:


Related :

This document entitled « VB6 - Adding a horizontal scroll in ListBox » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.