Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Comments

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!

License

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

Share

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.

Comments and Discussions

 
GeneralThank You PinmemberCompletenutter29-May-07 1:53 
GeneralAnother approach PinmemberNetSpore9-Oct-05 4:52 
Generalexcellent ~~~~~~ Pinmembergood_sir19-Sep-05 18:02 
GeneralDatagrid and scrollablecontrol Pinsusscyphe22-Aug-05 8:20 
Generalright on time PinmemberDaberElay10-Aug-05 22:38 
GeneralProportional Scrolling PinmemberKRA-Z7-Jun-05 5:22 
QuestionEvent do not fire? Pinmemberjaverty22-Jan-05 10:24 
AnswerRe: Event do not fire? Pinmember21Solutions8-Aug-05 12:09 
GeneralNo source PinmemberKcN33z29-Jun-04 8:41 
GeneralRe: No source PinmemberDaniel Danilin12-Jul-04 3:07 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 18 Jun 2004
Article Copyright 2004 by Martin Randall
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid