Click here to Skip to main content
15,885,309 members
Articles / Mobile Apps / Windows Mobile
Article

ScrollableControl with Scroll Events

Rate me:
Please Sign up or sign in to vote.
3.22/5 (21 votes)
17 Jun 20042 min read 88.5K   1.8K   22   10
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


Written By
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 Pin
Completenutter29-May-07 1:53
Completenutter29-May-07 1:53 
GeneralAnother approach Pin
NetSpore9-Oct-05 4:52
NetSpore9-Oct-05 4:52 
Generalexcellent ~~~~~~ Pin
good_sir19-Sep-05 18:02
good_sir19-Sep-05 18:02 
GeneralDatagrid and scrollablecontrol Pin
cyphE22-Aug-05 8:20
cyphE22-Aug-05 8:20 
Generalright on time Pin
DaberElay10-Aug-05 22:38
DaberElay10-Aug-05 22:38 
GeneralProportional Scrolling Pin
KRA-Z7-Jun-05 5:22
KRA-Z7-Jun-05 5:22 
QuestionEvent do not fire? Pin
javerty22-Jan-05 10:24
javerty22-Jan-05 10:24 
AnswerRe: Event do not fire? Pin
21Solutions8-Aug-05 12:09
21Solutions8-Aug-05 12:09 
GeneralNo source Pin
KcN33z29-Jun-04 8:41
KcN33z29-Jun-04 8:41 
GeneralRe: No source Pin
Daniel Danilin12-Jul-04 3:07
Daniel Danilin12-Jul-04 3: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.