Click here to Skip to main content
Licence 
First Posted 17 Jun 2004
Views 66,051
Bookmarked 22 times

ScrollableControl with Scroll Events

By | 17 Jun 2004 | Article
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

About the Author

Martin Randall

Web Developer

United Kingdom United Kingdom

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank You PinmemberCompletenutter21:53 9 May '07  
GeneralAnother approach PinmemberNetSpore4:52 9 Oct '05  
Generalexcellent ~~~~~~ Pinmembergood_sir18:02 19 Sep '05  
GeneralDatagrid and scrollablecontrol Pinsusscyphe8:20 22 Aug '05  
Generalright on time PinmemberDaberElay22:38 10 Aug '05  
GeneralProportional Scrolling PinmemberKRA-Z5:22 7 Jun '05  
QuestionEvent do not fire? Pinmemberjaverty10:24 22 Jan '05  
AnswerRe: Event do not fire? Pinmember21Solutions12:09 8 Aug '05  
GeneralNo source PinmemberKcN33z8:41 29 Jun '04  
GeneralRe: No source PinmemberDaniel Danilin3:07 12 Jul '04  

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.

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