Link to home
Start Free TrialLog in
Avatar of David L. Hansen
David L. HansenFlag for United States of America

asked on

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

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.
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

try setting the gotfocus to true when you load the form
Avatar of David L. Hansen

ASKER

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...
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..
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Idle_Mind....you rock!
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.
It should also work from the Shown() event instead of Load():

        private void Form1_Shown(object sender, EventArgs e)
        {
            panel1.Focus();
        }
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.