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?
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..
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;
}
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.
What worked for me was adding panel1_MouseEnter
EventHandler:
private void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.Focus();
}
Below code works for me.....
Public Form
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(Panel1_MouseWheel);
}
private void Panel1_MouseWheel(object sender, MouseEventArgs 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.