Enable and Disable all or part of a scrollbar

Enable and Disable all or part of a scrollbar

The new FlatScrollbar controls expose the ability to selectively disable their arrows. This is useful, for example, when the thumb indicator is at its minimum or maximum:

Private Sub FlatScrollBar1_Change()    If FlatScrollBar1.Value = FlatScrollBar1.Min Then        FlatScrollBar1.Arrows = cc2RightDown    ElseIf FlatScrollBar1.Value = FlatScrollBar1.Max Then        FlatScrollBar1.Arrows = cc2LeftUp    Else        FlatScrollBar1.Arrows = cc2Both    End IfEnd Sub

It turns out, however, that you can get the same effect even with standard scrollbars, by using the EnableScrollBar API function:

Private Declare Function EnableScrollBar Lib "user32" (ByVal hwnd As Long, _    ByVal wSBflags As Long, ByVal wArrows As Long) As LongPrivate Const SB_HORZ = 0   ' horizontal scrollbarPrivate Const SB_VERT = 1   ' vertical scrollbarPrivate Const SB_CTL = 2    ' scollbar controlPrivate Const SB_BOTH = 3   ' both horiz & vert scrollbarsPrivate Const ESB_ENABLE_BOTH = &H0   ' enable both arrowsPrivate Const ESB_DISABLE_LTUP = &H1  ' disable left/up arrowsPrivate Const ESB_DISABLE_RTDN = &H2  ' disable right/down arrowsPrivate Const ESB_DISABLE_BOTH = &H3  ' disable both arrowsPrivate Sub VScroll1_Change()    SetScrollbarArrows VScroll1End SubSub SetScrollbarArrows(sbar As Control)    Dim barType As Long    ' first, re-enable both arrows    EnableScrollBar sbar.hWnd, SB_CTL, ESB_ENABLE_BOTH    ' then disable one of them, if necessary    If sbar.Value = sbar.Min Then        EnableScrollBar sbar.hWnd, SB_CTL, ESB_DISABLE_LTUP    ElseIf sbar.Value = sbar.Max Then        EnableScrollBar sbar.hWnd, SB_CTL, ESB_DISABLE_RTDN    End IfEnd Sub

This solution has only a minor drawback. When you reach the Min or Max value by clicking on arrow keys, the Change event fires and correctly disable the corresponding arrow, but as soon as you release the mouse VB re-enables it. This isn’t a problem when you reach the Min or Max value by sliding the thumb indicator, or by using the arrow keys. If you want to be sure that you always get the correct result, just call the SetScrollbarArrows procedure from the Timer event procedure of a Timer control instead of the scrollbar’s Change event:

Private Sub Timer1_Timer()    SetScrollbarArrows VScroll1End Sub

Finally, note that you can use the EnableScrollBar API to enable or disable the scrollbars that are associated to any control, such as a ListBox, ComboBox or TreeView control. In this case you must use the 2nd argument to identify which scrollbar you want to enable or disable. For example:

' disable the vertical scrollbar of a ListBox controlEnableScrollBar List1.hWnd, SB_VERT, ESB_DISABLE_BOTH 


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist