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 

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may