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 

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes