Click here to Skip to main content
12,826,621 members (23,319 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 17 Jun 2004

ScrollableControl with Scroll Events

, 17 Jun 2004
Rate this:
Please Sign up or sign in to vote.
A ScrollableControl extension to generate horizontal and vertical scroll events.


If you are writing your own control, the chances are that you are going to be using the .NET framework class Control as a base class. If you are wanting to write a control that acts as a window onto a larger virtual area (such as a data grid or a drawing control), then ScrollableControl nicely encapsulates the handling of the scrollbars. So far so good - except ScrollableControl has one glaring omission. What if I want to track when my control has been scrolled?

I came across this problem when I was writing my own data grid control. My actual grid worked nicely in a ScrollableControl derived class, and I wanted to implement a column header in a separate control and combine them both into a Panel control object. I wanted my column header control to scroll horizontally when my data grid control scrolled horizontally. The problem was that the only way you can tell if the ScrollableControl derived control had been scrolled was to poll its AutoScrollPosition value. Not ideal! What I needed was a ScrollableControl that would send out scroll events encapsulating the WM_HSCROLL and WM_VSCROLL Windows messages.

The Solution - An Extended Class

In true object-orientated fashion, I derived a new class from the ScrollableControl class that would generate the extra events. The class contains event stubs for the two new scroll events, and overrides the WndProc method to intercept Windows messages sent to the control. We let the base class handle the message first so that it updates the scroll position of the the control before firing the event. Finally, we translate the event into a ScrollHandlerEvent notification as this is already provided in the .NET framework.


The ScrollableControlWithScrollEvents class can be used anywhere you have used the ScrollableControl class. A similar extension could be made to the Panel class if you wish to trap scroll events from that.

Have fun!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Martin Randall
Web Developer
United Kingdom United Kingdom
Martin Randall is a developer working for a small development company in North Yorkshire, UK. He's been working in C++ for 9 years and C# for since 2001. He recently qualified as an MCSD in .Net and is currently working on towards MCDBA and MCSE.

You may also be interested in...

Comments and Discussions

GeneralThank You Pin
Completenutter29-May-07 2:53
memberCompletenutter29-May-07 2:53 
GeneralAnother approach Pin
NetSpore9-Oct-05 5:52
memberNetSpore9-Oct-05 5:52 
Generalexcellent ~~~~~~ Pin
good_sir19-Sep-05 19:02
membergood_sir19-Sep-05 19:02 
GeneralDatagrid and scrollablecontrol Pin
cyphe22-Aug-05 9:20
susscyphe22-Aug-05 9:20 
Generalright on time Pin
DaberElay10-Aug-05 23:38
memberDaberElay10-Aug-05 23:38 
GeneralProportional Scrolling Pin
KRA-Z7-Jun-05 6:22
memberKRA-Z7-Jun-05 6:22 
QuestionEvent do not fire? Pin
javerty22-Jan-05 11:24
memberjaverty22-Jan-05 11:24 
AnswerRe: Event do not fire? Pin
21Solutions8-Aug-05 13:09
member21Solutions8-Aug-05 13:09 
GeneralNo source Pin
KcN33z29-Jun-04 9:41
memberKcN33z29-Jun-04 9:41 
GeneralRe: No source Pin
Daniel Danilin12-Jul-04 4:07
memberDaniel Danilin12-Jul-04 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 18 Jun 2004
Article Copyright 2004 by Martin Randall
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid