• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2144
  • Last Modified:

Determining position of horizontal scroll bar in a listbox

Hi Folks:

    I am trying to create an ActiveX component consisting of two listboxes. I need to be trap the position of the horizontal scrollbar in one of the listboxes so as to be able to then set the position of the horizontal scrollbar in the second listbox to match.

I have code which subclsses and traps the horizontal scroll bar event in the first listbox, but how can I determine its position and then update the position of the scrollbar in the second listbox?

I have been looking at GetScrollinfo / SetScrollinfo Win API calls, but simply including the declaration statements in the userControl seems to cause a VB 6.0 GPF fault - blows away VB6.0 environment!

Any assistance would be greatly appreciated.

Best regards, Dave Melnyk, Toronto
1 Solution
I don't know about the scroll bars but, if the list boxes have an equal number of items, surely you can just set the SelectedItem value of one to the SelectedItem value of the other.

Or are you just trying to smoothly scroll both at the same time, without selection?

The below should work. Replace SB_VERT and WM_VSCROLL to their opposites (tested with vertical scrolling).

Option Explicit

Private Declare Function GetScrollPos Lib "user32" (ByVal hwnd As Long, ByVal nBar As Long) As Long
Private Declare Function SetScrollPos Lib "user32" (ByVal hwnd As Long, ByVal nBar As Long, ByVal nPos As Long, ByVal bRedraw As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const SB_HORZ = 0
Private Const SB_THUMBPOSITION = 4
Private Const SB_VERT = 1
Private Const WM_HSCROLL = &H114
Private Const WM_VSCROLL = &H115
Private Sub Form_Load()
    'Just add some items to the listboxes..
    Dim intLoop As Integer
    For intLoop = 1 To 50
        Call List1.AddItem(intLoop)
        Call List2.AddItem(intLoop)
    Next intLoop
End Sub
Private Sub List1_Scroll()
    Dim lngScrollPos As Long
    'get scroll pos of list1...
    lngScrollPos = GetScrollPos(List1.hwnd, SB_VERT)
    'set scroll pos of list2 to match list1...
    Call SetScrollPos(List2.hwnd, SB_VERT, lngScrollPos, True)
    'post message to list2 for it to update the items shown at
    'this scroll pos..
    Call PostMessage(List2.hwnd, WM_VSCROLL, SB_THUMBPOSITION + &H10000 * lngScrollPos, 0)
End Sub
Mike TomlinsonMiddle School Assistant TeacherCommented:
That technique will work but you still have to subclass the form to get the Horizontal Scroll event since the List1_Scroll() event won't fire for horizontal movement.

d_melnykAuthor Commented:
Jimbobmcgee:   Thanks for the reply. I am trying to scroll listboxes horizontally without selection.

zzzzz00c: Thanks for the reply and the sample code. Tried it with the few small changes for horizontal scrolling and it works great! Many thanks ... we have a winner!

Idle Mind: Thanks for the reply. I had mentioned in my question that I had found some subclassing code to trap the horizontal scroll event - found the  artical and sample code at www.devx.com/tips/Tip/5333 in case anyone else reading this is interested.

Many thanks to all who responded - you have helped me to keep my sanity intact.

Best regards, Dave Melnyk

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now