Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Determining position of horizontal scroll bar in a listbox

Posted on 2004-10-25
Medium Priority
Last Modified: 2012-08-14
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
Question by:d_melnyk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 12405214
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?

LVL 17

Accepted Solution

zzzzzooc earned 600 total points
ID: 12405392
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
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12405482
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.


Author Comment

ID: 12405898
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question