How can I scroll my panel using my mousewheel?
Asked Answered
M

9

38

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.

Majewski answered 21/10, 2009 at 12:57 Comment(0)
S
31

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.

Spillway answered 21/10, 2009 at 13:10 Comment(7)
Hi John, so I just set the Focus() to the panel controller and it should scroll?Majewski
Exactly. You can call panel1.Focus() or just click on something in the panel and it will scroll.Spillway
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.Milliemillieme
@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.Spillway
@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.Milliemillieme
@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.Spillway
Simply setting focus (as with the method in beam022's answer doesn't enable scrolling for me either. I have AutoScroll = true for the panel.Brachycephalic
V
58

What worked for me was adding panel1_MouseEnter EventHandler:

private void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.Focus();
}
Varitype answered 31/1, 2012 at 20:52 Comment(4)
thank you, this simpler solution worked for me. I have a picture box which is filled inside the panel. Instead of panel mouse enter. i did picturebox mouse enter which resolved my issue.Unimpeachable
This can create a very unexpected behavior, if you click into a textBox or want to edit something else this will focus the panel if you hover into it. I would not do this.. (just my opinion)Owner
This did not work for me, as the panel was completely occupied by controls that did not pass the event upwards to the parent, so the panel never got the mouse enter event in the first place.Liselisetta
Excellent, Briliant & Fantastic SolutionVersieversification
S
31

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.

Spillway answered 21/10, 2009 at 13:10 Comment(7)
Hi John, so I just set the Focus() to the panel controller and it should scroll?Majewski
Exactly. You can call panel1.Focus() or just click on something in the panel and it will scroll.Spillway
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.Milliemillieme
@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.Spillway
@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.Milliemillieme
@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.Spillway
Simply setting focus (as with the method in beam022's answer doesn't enable scrolling for me either. I have AutoScroll = true for the panel.Brachycephalic
P
17

Below code works for me.....

    Public Form
{
InitializeComponent();  
this.MouseWheel += new MouseEventHandler(Panel1_MouseWheel);
}

 private void Panel1_MouseWheel(object sender, MouseEventArgs e)
        {
         panel1.Focus();
         }
Phytoplankton answered 22/1, 2011 at 5:58 Comment(1)
This is excellent. More succinctly, in the parent form's constructor: MouseWheel += (s, e) => myPanel.Focus();Blintz
C
3

Make sure that your panel has focus. And this is simple code to scroll your panel scrollbar.

int deltaScroll = 10;

if (e.Delta > 0)
{
    
    if (pnlContain.VerticalScroll.Value - deltaScroll >= pnlContain.VerticalScroll.Minimum)
        pnlContain.VerticalScroll.Value -= deltaScroll;
    else
        pnlContain.VerticalScroll.Value = pnlContain.VerticalScroll.Minimum;
}
else
{
    if (pnlContain.VerticalScroll.Value + deltaScroll <= pnlContain.VerticalScroll.Maximum)
        pnlContain.VerticalScroll.Value += deltaScroll;
    else
        pnlContain.VerticalScroll.Value = pnlContain.VerticalScroll.Maximum;
}
Cwmbran answered 25/6, 2013 at 14:31 Comment(0)
P
2

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

Potsherd answered 8/12, 2009 at 5:14 Comment(1)
For posterity - the MouseWheel event will not be triggered unless a control on the panel has focus, as stated in the accepted answer. At that point, the panel will scroll automatically - but this event will then be viable for further customization.Mechanics
M
0

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.

Milliemillieme answered 21/10, 2009 at 13:14 Comment(1)
Just for posterity - This does indeed trigger the MouseMove event, but the Delta property (though its IntelliSense agrees with your statement above) is always 0, from what I can tell. More here - social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/…Mechanics
T
0

I am using a windows form with BorderStyle set to none, where I use a panel to have all my controls in, so it looks nice (color difference and such..) was having the same issue while I had other forms that worked fine.

What did I forgot:

   public myForm()
   {
        InitializeComponent();
        this.DoubleBuffered = true;
   }

DoubleBuffered is magical I noticed..

Triclinic answered 24/12, 2014 at 19:26 Comment(0)
S
0

The solution (seen above) provided by Beam022 worked for me while many of the other solutions did not. In my case, I was attempting to scroll a DataGridView control with the mousewheel event.

The DataGridView_MouseWheel event handler was being called but the FirstDisplayedScrollingRowIndex value never changed. The value was always '0' even after explicitly setting it to 1. It's as if the property were read only.

Still repro's in .Net Framework 4.6.

Scarberry answered 5/8, 2015 at 23:17 Comment(1)
Realized that I don't even need to use special code to scroll grids with the mouse wheel so I was able to remove a bunch of unnecessary code including the problematic FirstDisplayedScrollingRowIndex. The act of setting focus to the DataGridView control is enough.Scarberry
L
0

In my case, the whole client area of the panel was occupied by UserControls (not a single pixel of the inner area visible, except the scrollbars).

In this case the panel doesn't get the mouse-events and will never focus (apperently, clicking on the scrollbar does not count as "being inside the panel").

I had to add the following lines to the constructor of my UserControl derived class:

MouseEnter += delegate {
   Parent?.Focus();
};

Now it works fine, as I have no scrollable content in the UserControls.

Liselisetta answered 23/1, 2018 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.