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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.