I need to allow the vertical scrollbar in a multiselect listbox (VB6) however, when the control is disabled, I can’t scroll.
I would think there is an API to allow this, but my favorite VB6 site (MVPS VB.NET) does not have a way.
I toyed with pretending it was disabled, and ignore the clicks… but to do that with VB6 code is really ugly… so if this is a solution, I need an API to ignore the clicks.
Thanks for your help.
I came up with the following code, which hides all of the gnarly details behind a class. Basically, I implemented greg’s idea of using overlaying another scrollbar on top of the disabled list box’s scrollbar. In my code, I dynamically create another ListBox control (resized so that only its scrollbar is visible), and use its scrollbar to scroll the actual ListBox. I also specifically avoided using the Windows API (except for the call to
GetSystemMetricsthat I used to figure how how wide a scroll bar is on the system). The nice thing about using another ListBox’s scrollbar is that it will be themed properly (a ListBox uses the OS’s theme when it displays it’s scrollbar, but a VB.Scrollbar doesn’t, so it would look out-of-place). Another advantage of using a second ListBox to scroll the first list box is that it’s really easy to implement the scrolling logic (just set the first ListBox’s TopIndex property to the second ListBox’s TopIndex property whenever the second one is scrolled).I also set it up to be as low-impact as possible (you only have to call a single function in your
Form_Loadevent to make it work).Usage
Add
CustomScrollingSupport.clsandListBoxExtras.basto your project.In your form’s
Form_Loadevent, add the following line:AddCustomListBoxScrolling MeThis will make every VB.ListBox on the form support scrolling even while they are disabled. If you only want to add this functionality to a select number of ListBox’s, you can call
AddCustomScrollingSupportinstead, passing in a specific ListBox control.Interesting Note
In an older version of this code, I wasn’t calling the
ZOrdermethod on the second listbox (the one that provides the scrollbar) to make sure it would appear on top of the first listbox. This meant the second listbox was actually behind the first listbox; the interesting thing is that the scrolling on the second ListBox still worked when the first ListBox was disabled! Apparently, when the first ListBox is disabled, any mouse and keyboard events that would have gone to that ListBox ‘bleed through’ to the second ListBox, so scrolling support still does work. I’m not sure if this is a bug or by design (I mean, you could argue that it makes sense that controls behind a disabled control would be able to receive events…). However, I found the scrolling to be slightly jerky at times, so I decided to add.ZOrder 0to make the second listbox render on top of the first one. This has the drawback that you see the frame border for the second listbox (to the left of the scroll bar), which you wouldn’t see if it was hidden behind the first listbox, but the scrolling is smoother.CustomScrollingSupport.cls
This class wraps up the logic necessary to add ‘custom scrolling support’ (for lack of a better name) to a
VB.ListBoxcontrol. It should not be used directly, instead use the one of theAdd*methods in theListBoxExtras.basmodule (I’ll provide the code for that module later in the post).ListBoxExtras.bas
This module contains two utility methods:
AddCustomScrollingSupportadds custom scrolling functionality to an individualVB.ListBoxcontrolAddCustomListBoxScrollingadds custom scrolling functionality to everyVB.ListBoxcontrol on a givenForm