Click here to Skip to main content
12,897,945 members (83,225 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 23 Dec 2009

Scroll Bar Autoscroll Helper

, 23 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a module that will automatically scroll a control that is passed to it.


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


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.

Public Sub Scroll(ByVal ControlToScroll As Control, _ 
      ByVal Direction As ScrollDirection, _
      Optional ByVal Speed As ScrollSpeed = ScrollSpeed.Line)
    Dim ctrlOriginalFocus As Control = _
    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
    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

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.


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


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


About the Author

Software Developer Wynright
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 23 Dec 2009
Article Copyright 2009 by cjbarth
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid