How can I get scrolling to work in my Panel control?

David L. Hansen
David L. Hansen used Ask the Experts™
on
I have a panel control on a form (Panel1).  Its AutoScroll value is set to True but it won't scroll with the mouse wheel unless I click on a control inside the panel first.  I'd like the user to be able to start scrolling with the mouse wheel right away.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try setting the gotfocus to true when you load the form
done that...no joy.
The panel control can receive messages only if it is focused or is active control. The Main window eats up the scroll message
At the begining of the application the panel is not focused but as you click on controls inside it it starts receiving message and hence scrolls.
Try to set focus on the panel programatically or set it as active control in the form load event.
panel1.
try            
  panel1.Select();
  panel1.Focus();

Thx!
Swaps...
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Rufus WilliamSenior Software Architect

Commented:
the panel do not have focus or select property. you have to select / focus the one of the controls inside the panel...preferably the first one..
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Handle the MouseEnter() event of your control and give it focus there:

        private void panel1_MouseEnter(object sender, EventArgs e)
        {
            panel1.Focus();
        }
Idle_Mind....you rock!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Nah...to be fair, both planocz and swaps also suggested Focus().  I just added the MouseEnter() part...  ;)
Not sure why, but using focus on either the inner control or panel1 just didn't work.  I placed it as the last thing to happen in the Load event...but got nowhere.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
It should also work from the Shown() event instead of Load():

        private void Form1_Shown(object sender, EventArgs e)
        {
            panel1.Focus();
        }
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I like to keep the MouseEnter() event code too, though, as it seems that users quickly learn they can just move the mouse over the panel and start scrolling without needing to click.  This can be useful if you have more than one panel on the form that needs this behavior...

If you want to intercept the MouseWheel() regardless of which control has focus and make the panel scroll then you can implement IMessageFilter() and make it notify the form so you can take action...lemme know if you want to see that.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial