Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ScrollableControl with Scroll Events

0.00/5 (No votes)
17 Jun 2004 1  
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