Click here to Skip to main content
15,503,808 members
Articles / Programming Languages / C#
Tip/Trick
Posted 6 Jan 2011

Tagged as

Stats

29.3K views
2 bookmarked

How to scroll a parent control with mouse wheel while child control has focus

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Jan 2011CPOL1 min read
How to scroll a parent control with mouse wheel while child control has focus
I have been working on a custom container control. I wanted the parent autoscroll inherited control to scroll with the mouse wheel, but when a contained control had focused, this wouldn't happen because the child control received the mouse events.

Many people use Windows APIs for this, to tell the window to scroll, but I found that tedious and the results weren't always preferable. After thinking about it, I realized that you could just call the container controls onMouseWheel base class method directly, and pass to it the mouse event args from the child control.

C#
public void doMouseWheel(MouseEventArgs e)
{
    base.OnMouseWheel(e);
}



Simply creating a public method that handles calling the base class to our container control and for simplicity giving it an interface that shows it has this method:

C#
public interface IMouseable
{
    void doMouseWheel(MouseEventArgs e);
}


Then in our child control, we just override its onMouseWheel event and call our parent IMouseable object's doMouseWheel method.

C#
//capture mouse wheel events to ride them up to the host
protected override void OnMouseWheel(MouseEventArgs e)
{
    base.OnMouseWheel(e); //if you need local mouse wheel events
         
      //cast the parent to IMouseable and call its doMouseWheel mothod.
        ((IMouseable)(Parent)).doMouseWheel(e);
}

Yes, I do realize that is a messy cast, but this code is for educational purposes only.

And it's that simple, No API calls, no fancy event handling (although you could technically just subscribe to the child contols' onMouseWheel event and call the base class method that way, but this example doesn't show the reasons why it is simpler to use the interface. In my production code, there is actually a ladder of controls below the container control, and all of them could have focus and should taxi the scroll event upward to the parent.

It's not the only way, it might not be the best way, But I am very satisfied with the results of it so far.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I am a part time programmer, The more I learn, the more I need to learn, the best of which I will share here on codeproject!

Visit my site and blog

Comments and Discussions

 
QuestionCast error occured Pin
MVSoftVM18-Nov-21 4:02
MVSoftVM18-Nov-21 4:02 
PraiseThumbs up! Pin
wasabi_sydney22-Jul-18 20:10
wasabi_sydney22-Jul-18 20:10 
PraiseThank you Pin
Z-User22-Jun-18 7:34
Z-User22-Jun-18 7:34 
QuestionHow woud you do the opposite, ie. pass MouseWheel from parent to child control? Pin
cp2k39-May-13 23:02
cp2k39-May-13 23:02 
AnswerRe: How woud you do the opposite, ie. pass MouseWheel from parent to child control? Pin
Diamonddrake2-May-14 7:59
Diamonddrake2-May-14 7:59 
Just do the opposite, make a public method on the child that calls it's OnMouseWheel(e) directly, then on the event for the parent call that public method of the child.

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.