How can I scroll my panel using my mousewheel?
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.
What worked for me was adding panel1_MouseEnter
EventHandler:
private void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.Focus();
}
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.
Below code works for me.....
Public Form
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(Panel1_MouseWheel);
}
private void Panel1_MouseWheel(object sender, MouseEventArgs e)
{
panel1.Focus();
}
Make sure that your panel has focus. And this is simple code to scroll your panel scrollbar. Hope this help. :) enter code here
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;
}
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
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.
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..
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.
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.
精彩评论