Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Oct 30, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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
        FlatScrollBar1.Arrows = cc2Both
    End If
End 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 Long
Private Const SB_HORZ = 0   ' horizontal scrollbar
Private Const SB_VERT = 1   ' vertical scrollbar
Private Const SB_CTL = 2    ' scollbar control
Private Const SB_BOTH = 3   ' both horiz & vert scrollbars

Private Const ESB_ENABLE_BOTH = &H0   ' enable both arrows
Private Const ESB_DISABLE_LTUP = &H1  ' disable left/up arrows
Private Const ESB_DISABLE_RTDN = &H2  ' disable right/down arrows
Private Const ESB_DISABLE_BOTH = &H3  ' disable both arrows

Private Sub VScroll1_Change()
    SetScrollbarArrows VScroll1
End Sub

Sub 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 If
End 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 VScroll1
End 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 control
EnableScrollBar List1.hWnd, SB_VERT, ESB_DISABLE_BOTH 
Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date