none
How do you disallow any scrolling of a listbox control RRS feed

  • Question

  • If you can't prevent scrolling with the scrollbar, can you at least prevent scrolling with the mouse wheel?

    Robert Homes

    Monday, October 16, 2017 2:20 AM

Answers

  • Maybe subclass and ignore those messages,..

    Public Class ListBoxEx
        Inherits ListBox
    
        Private Const WM_HSCROLL As Int32 = &H114
        Private Const WM_VSCROLL As Int32 = &H115
        Private Const WM_MOUSEWHEEL As Int32 = &H20A
    
        Public Property Lock As Boolean = True
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_MOUSEWHEEL, WM_VSCROLL, WM_HSCROLL
                    If Lock Then Return
            End Select
            MyBase.WndProc(m)
        End Sub
    
    End Class


    • Edited by Edgemeal Monday, October 16, 2017 4:05 AM Remove unneeded Imports
    • Proposed as answer by IronRazerz Monday, October 16, 2017 3:52 PM
    • Marked as answer by Robert Homes Wednesday, October 25, 2017 4:32 PM
    Monday, October 16, 2017 3:56 AM

All replies

  • Maybe subclass and ignore those messages,..

    Public Class ListBoxEx
        Inherits ListBox
    
        Private Const WM_HSCROLL As Int32 = &H114
        Private Const WM_VSCROLL As Int32 = &H115
        Private Const WM_MOUSEWHEEL As Int32 = &H20A
    
        Public Property Lock As Boolean = True
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_MOUSEWHEEL, WM_VSCROLL, WM_HSCROLL
                    If Lock Then Return
            End Select
            MyBase.WndProc(m)
        End Sub
    
    End Class


    • Edited by Edgemeal Monday, October 16, 2017 4:05 AM Remove unneeded Imports
    • Proposed as answer by IronRazerz Monday, October 16, 2017 3:52 PM
    • Marked as answer by Robert Homes Wednesday, October 25, 2017 4:32 PM
    Monday, October 16, 2017 3:56 AM
  •  What would be the purpose of stopping the user from scrolling the listbox if it contains more items than can be seen?

     Maybe try just setting the ListBox.Enabled property to False if or when you don't want the user to interact with the ListBox.  Set it back to True if or when you want them to be able to interact with it again.


    If you say it can`t be done then i`ll try it

    Monday, October 16, 2017 3:51 PM
  • It still seems odd why you would want to do this but,  figured i would add this....

     If you use Edgemeal`s example,  you may also want to ignore the 4 arrow keys and the PageUp and PageDown keys or the user will still be able to scroll the listbox with them when it has focus.

    Public Class ListBoxEx
        Inherits ListBox
    
        Private Const WM_HSCROLL As Int32 = &H114
        Private Const WM_VSCROLL As Int32 = &H115
        Private Const WM_MOUSEWHEEL As Int32 = &H20A
        Private Const WM_KEYDOWN As Int32 = &H100
    
        Public Property Lock As Boolean = True
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_MOUSEWHEEL, WM_VSCROLL, WM_HSCROLL
                    If Lock Then Return
                Case WM_KEYDOWN
                    '                              (37 <--> 40 are the arrow keys)                       (33 and 34 are the PageUp and PageDown keys)
                    If Lock AndAlso ((m.WParam.ToInt32 >= 37 AndAlso m.WParam.ToInt32 <= 40) OrElse (m.WParam.ToInt32 = 33 OrElse m.WParam.ToInt32 = 34)) Then Return
            End Select
            MyBase.WndProc(m)
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    Monday, October 16, 2017 5:14 PM
  • IronMan,

    I made a video to demonstrate my issue and explain why I want to do this, but it wouldn't upload, the thing only takes still images. Anyway, I WANT users to be able to move up and down in the two lists (one is for "Categories" (subfolders), the other for "Files" (just filenames). But when they move up or down in one list, the other list has to move up and down with the first list ("synced"). That works fine as long as users use the keyboard to move up and down (I can use the "selectionchanged" event for each list to move the other list in sync with the first, or vice-versa. That also works for mouse-clicking in a list (that also triggers the selectingchanged event.) But I can't keep them in sync when the user scrolls one of the lists -- there seems to be no event to catch the scrolling action and use it to move the other list.

    I got in touch with DX and they did agree to help me even though my maintenance period expired. And they did help me with this -- "Igor" send me some code to create a list type from their original "listbox control" which will catch scroll events. So I'm almost there.

    Thanks for all you help!!


    Robert Homes

    Monday, October 16, 2017 7:28 PM
  • IronMan,

    I made a video to demonstrate my issue and explain why I want to do this, but it wouldn't upload, the thing only takes still images. Anyway, I WANT users to be able to move up and down in the two lists (one is for "Categories" (subfolders), the other for "Files" (just filenames). But when they move up or down in one list, the other list has to move up and down with the first list ("synced"). That works fine as long as users use the keyboard to move up and down (I can use the "selectionchanged" event for each list to move the other list in sync with the first, or vice-versa. That also works for mouse-clicking in a list (that also triggers the selectingchanged event.) But I can't keep them in sync when the user scrolls one of the lists -- there seems to be no event to catch the scrolling action and use it to move the other list.

    I got in touch with DX and they did agree to help me even though my maintenance period expired. And they did help me with this -- "Igor" send me some code to create a list type from their original "listbox control" which will catch scroll events. So I'm almost there.

    Thanks for all you help!!


    Robert Homes

     Yes,  now i remember trying to help you with this the other day.  If you are using DevExpress controls,  you should at least mention that in your questions because this forum is not for DevExpress controls.  It would avoid everyone looking at it and giving solutions for the standard Vb.Net controls.  The best place to get help with 3rd party controls is with the creators of the 3rd party controls.  8)

     

    If you say it can`t be done then i`ll try it

    Monday, October 16, 2017 7:52 PM
  • It still seems odd why you would want to do this but,  figured i would add this....

     If you use Edgemeal`s example,  you may also want to ignore the 4 arrow keys and the PageUp and PageDown keys or the user will still be able to scroll the listbox with them when it has focus.


    Nice addition!
    Hmmm, If you click on an item and hold mouse button down and then move the mouse pointer above the top (or below the bottom) of the list, the control will auto scroll the items also! Not sure how you'd handle that, doubt I'd ever use it, but just curious, any thoughts?
    Tuesday, October 17, 2017 12:04 AM
  • It still seems odd why you would want to do this but,  figured i would add this....

     If you use Edgemeal`s example,  you may also want to ignore the 4 arrow keys and the PageUp and PageDown keys or the user will still be able to scroll the listbox with them when it has focus.


    Nice addition!
    Hmmm, If you click on an item and hold mouse button down and then move the mouse pointer above the top (or below the bottom) of the list, the control will auto scroll the items also! Not sure how you'd handle that, doubt I'd ever use it, but just curious, any thoughts?

     Hmmm... good catch.  I did not think about testing dragging the mouse like that.  Handling the WM_MOUSEMOVE message and checking if the wParam for the left mouse button is down (MK_LBUTTON) seems to stop that.  8)

    Public Class ListBoxEx
        Inherits ListBox
    
        Private Const WM_HSCROLL As Int32 = &H114
        Private Const WM_VSCROLL As Int32 = &H115
        Private Const WM_MOUSEWHEEL As Int32 = &H20A
        Private Const WM_KEYDOWN As Int32 = &H100
        Private Const WM_MOUSEMOVE As Int32 = &H200
        Private Const MK_LBUTTON As Int32 = &H1
    
        Public Property Lock As Boolean = True
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_MOUSEWHEEL, WM_VSCROLL, WM_HSCROLL
                    If Lock Then Return
    
                Case WM_KEYDOWN
                    '                              (37 <--> 40 are the arrow keys)                       (33 and 34 are the PageUp and PageDown keys)
                    If Lock AndAlso ((m.WParam.ToInt32 >= 37 AndAlso m.WParam.ToInt32 <= 40) OrElse (m.WParam.ToInt32 = 33 OrElse m.WParam.ToInt32 = 34)) Then Return
    
                Case WM_MOUSEMOVE
                    If Lock andalso m.WParam.ToInt32 = MK_LBUTTON Then Return
    
            End Select
            MyBase.WndProc(m)
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, October 17, 2017 12:47 AM
    Tuesday, October 17, 2017 12:33 AM
  • Hi Robert,

    According to IronRazerz's  post, can you tell me that you use vb.net control or DevExpress controls, because this forum is not supported 3rd party controls.

    Thanks for your understanding.

    Best regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 25, 2017 7:26 AM
    Moderator