Click here to Skip to main content
15,886,032 members
Articles / Programming Languages / Visual Basic

Scroll Bar Autoscroll Helper

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
23 Dec 2009CPOL2 min read 20.5K   325   10  
This is a module that will automatically scroll a control that is passed to it.

Introduction

This is a simple tool to allow automatic scrolling of any control.

Background

Often, when working in Windows Forms, there are controls that we are adding things to periodically, and we would like those controls to automatically scroll to the top-most or bottom-most item in the list. This module was written to facilitate that.

Using the code

To get a control to scroll, simply pass the control you want to scroll to the Scroll routine, along with the direction and, optionally, the speed to scroll (e.g., one line, one page, all items). Of course, you'll have to make sure to add the DLL to your project first.

How the code works

The routine takes in a control and checks to see if it is a DataGridView. If it is a DataGridView, then the control that we actually send the scroll message to is different than for other controls because there can be multiple visible scroll bars in a DataGridView control.

A message is then sent to Windows to simulate a click on the appropriate part of the scroll bar.

After the scroll operation is complete, the focus is returned to the control that previously had the focus so that the focus doesn't appear to the user to have unexpectedly changed. The only problem is that the controls will register LostFocus and GotFocus events, if applicable.

Below is the code that does the heavy lifting. You'll notice that the speed is optional, and is set to scroll a single line at a time.

VB
Public Sub Scroll(ByVal ControlToScroll As Control, _ 
      ByVal Direction As ScrollDirection, _
      Optional ByVal Speed As ScrollSpeed = ScrollSpeed.Line)
    Dim ctrlOriginalFocus As Control = _
          FindFocus(ControlToScroll.TopLevelControl)
    Dim lParam As IntPtr = IntPtr.Zero

    If TypeOf ControlToScroll Is System.Windows.Forms.DataGridView Then
        For Each ctrl As Control In ControlToScroll.Controls
            If ctrl.Visible Then
                If TypeOf ctrl Is VScrollBar AndAlso _
                  (Direction = ScrollDirection.Down Or _
                  Direction = ScrollDirection.Up) _
                  Or TypeOf ctrl Is HScrollBar AndAlso _
                  (Direction = ScrollDirection.Left Or _
                  Direction = ScrollDirection.Right) Then
                    lParam = ctrl.Handle
                    Exit For
                End If
            End If
        Next
    End If

    Select Case Direction
        Case ScrollDirection.Up, ScrollDirection.Down
            SendMessage(ControlToScroll.Handle, WM_VSCROLL, Direction + Speed, lParam)
            SendMessage(ControlToScroll.Handle, WM_VSCROLL, SB_ENDSCROLL, lParam)
        Case ScrollDirection.Left, ScrollDirection.Right
            SendMessage(ControlToScroll.Handle, WM_HSCROLL, Direction + Speed, lParam)
            SendMessage(ControlToScroll.Handle, WM_HSCROLL, SB_ENDSCROLL, lParam)
    End Select

    ctrlOriginalFocus.Focus()
End Sub

Points of interest

This is simple enough code, but it is nice to have it in a DLL that can be linked and then used whenever you need to scroll something automatically.

I've heard that there are some controls that might not scroll using the method used above. If you find a control like that, let me know and I'll update this code.

History

  • 22-Dec-2009 - v1.0.1 - Fixed a spelling mistake.
  • 22-Dec-2009 - v1.0.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Fortna
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --