Solved

Determining position of horizontal scroll bar in a listbox

Posted on 2004-10-25
2,018 Views
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
0
Question by:d_melnyk
    4 Comments
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    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?

    J.
    0
     
    LVL 17

    Accepted Solution

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


    Form1:
    ---------------
    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
    0
     
    LVL 85

    Expert Comment

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

    ~IM
    0
     

    Author Comment

    by:d_melnyk
    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
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    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…

    845 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

    Need Help in Real-Time?

    Connect with top rated Experts

    3 Experts available now in Live!

    Get 1:1 Help Now