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.

Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254

9 Answers9

59

What worked for me was adding panel1_MouseEnter EventHandler:

private void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.Focus();
}
beam022
  • 1,793
  • 4
  • 20
  • 27
  • 2
    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. – Esen Oct 17 '12 at 17:27
  • 7
    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) – quadroid Apr 24 '14 at 13:58
  • 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. – Xan-Kun Clark-Davis Jan 23 '18 at 02:51
  • Excellent, Briliant & Fantastic Solution – Zujaj Misbah Khan Feb 26 '20 at 08:50
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.

Jon B
  • 51,025
  • 31
  • 133
  • 161
  • 1
    Hi John, so I just set the Focus() to the panel controller and it should scroll? – Sergio Tapia Oct 21 '09 at 13:12
  • Exactly. You can call panel1.Focus() or just click on something in the panel and it will scroll. – Jon B Oct 21 '09 at 13:13
  • 1
    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 Oct 21 '09 at 13:19
  • @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 Oct 21 '09 at 13:23
  • 1
    @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 Oct 21 '09 at 13:24
  • @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 Oct 21 '09 at 13:27
  • 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. – Al Lelopath Nov 20 '14 at 16:12
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();
         }
Nivas
  • 295
  • 5
  • 11
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;
}
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
Nguyen Minh Hien
  • 455
  • 7
  • 10
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

Pavan Navali
  • 242
  • 3
  • 14
  • 3
    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. – Chris Barlow Apr 18 '11 at 21:48
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.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • 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 - http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/98029bb7-7a0b-432d-95eb-4a3160df5ab0 – Chris Barlow Apr 18 '11 at 21:46
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..

CularBytes
  • 9,924
  • 8
  • 76
  • 101
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.

Community
  • 1
  • 1
GrayDwarf
  • 2,469
  • 2
  • 20
  • 22
  • 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. – GrayDwarf Aug 05 '15 at 23:36
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.

Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38