Results 1 to 4 of 4

Thread: Listbox scrolling

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    31

    Listbox scrolling

    I have a listbox where I'm adding items as my applications runs over time.

    However, I'm not getting any horizontal scrollbars even though the size of my text entries is longer than my listbox width. I do get vertical scrollbars when my list grows bigger than the listbox's height, but nothing for horizontal scrolling.

    What do I need to do?

    Thanks.

  2. #2
    Junior Member
    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    22
    The normal ListBox VB doesn't have a horizontal scroll bar. To add a horizontal scroll bar to the control, you can call the Windows API SendMessage function with the LB_SETHORIZONTALEXTENT constant.

    Sample Code

    Option Explicit

    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

    Private Sub Command1_Click()
    Dim s As String
    Static x As Long
    s = InputBox("Please enter any text", "List scroll", _
    "this is a simple scrollbar sample for demonstration purposes")
    List1.AddItem s
    If x < TextWidth(s & " ") Then
    x = TextWidth(s & " ")
    If ScaleMode = vbTwips Then _
    x = x / Screen.TwipsPerPixelX ' if twips change to pixels
    SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, x, 0
    End If
    End Sub
    "A child of five could understand this. Fetch me a child of five." Groucho Marx

  3. #3
    Addicted Member
    Join Date
    Feb 2004
    Posts
    155
    Originally posted by JOW
    The normal ListBox VB doesn't have a horizontal scroll bar. To add a horizontal scroll bar to the control, you can call the Windows API SendMessage function with the LB_SETHORIZONTALEXTENT constant.

    Sample Code

    Option Explicit

    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

    Private Sub Command1_Click()
    Dim s As String
    Static x As Long
    s = InputBox("Please enter any text", "List scroll", _
    "this is a simple scrollbar sample for demonstration purposes")
    List1.AddItem s
    If x < TextWidth(s & " ") Then
    x = TextWidth(s & " ")
    If ScaleMode = vbTwips Then _
    x = x / Screen.TwipsPerPixelX ' if twips change to pixels
    SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, x, 0
    End If
    End Sub
    Is there a Not-Normal ListBox I can use without all this code? I'm not sure where to place all this code.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Lets make it easier to add:

    VB Code:
    1. 'goes in to a form
    2. Option Explicit
    3.  
    4. Private Declare Function SendMessageByNum Lib "user32" _
    5. Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
    6. wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    7. Const LB_SETHORIZONTALEXTENT = &H194
    8. Private Sub Form_Load()
    9.     List1.AddItem "This is very long text here and it doesn't fit into the listbox because it is so darn long!"
    10.     SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, 800, 0
    11. End Sub

    Make a sample project with that code and add a listbox. Then see how it works. 800 = 800 pixels.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width