views:

510

answers:

3

I have a panel on my form with AutoScroll set to true so a scrollbar appears automatically.

How can I make it so a user can use his mouse wheel to scroll the panel? Thanks SO.

+4  A: 

The panel or a control in the panel must have focus. Note that if the control with focus has scroll bars, it will scroll instead of the panel.

Jon B
Hi John, so I just set the Focus() to the panel controller and it should scroll?
Sergio Tapia
Exactly. You can call panel1.Focus() or just click on something in the panel and it will scroll.
Jon B
I just tried this and it doesn't work. The mouse wheel has no effect on the panel's scrollbars. Maybe I'm doing something wrong.
MusiGenesis
@MusiGenesis: create a project with a panel set to autoscroll with a button in it. Add another control well below the button and size the panel so you can't see the second control. At runtime, click the button (to make the panel have focus) and you'll be able to scroll. You could also use panel.Focus() instead of clicking the button inside it.
Jon B
@Jon B: I did all that, and still no scrolling effect from the wheel. I *am* able to scroll up and down with the wheel in IE, so I know the wheel is working.
MusiGenesis
@MusiGenesis: That's strange. It works fine for me. Either something is stealing the focus, or it's some bizzare mouse wheel problem. I've had the whell stop working in some apps until I reboot - it's obviously less than perfect.
Jon B
A: 

Moving the scroll wheel should trigger the control's MouseMove event. The MouseEventArgs argument has a property named Delta, which gives the (signed) number of notches that the mouse wheel has moved. You can use this property to scroll the panel.

MusiGenesis
A: 

In the designer file, you can add the following line of code. the MouseWheel event is not doumented in Events list in the Properties window.

this.Panel1.MouseWheel+= System.Windows.Forms.MouseEventHandler(this.Panel1_MouseWheel);

Panel1_MouseWheel will be triggered when you roll the mouse weel

Add the code in the .cs file

Pavan Navali