VB6 - Adding a horizontal scroll in ListBox

April 2017


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


Published by jak58. Latest update on March 9, 2011 at 03:48 AM by Jeff.
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).