How can I scroll my panel using my mousewheel?

2019-02-21 07:00发布

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.

9条回答
叛逆
2楼-- · 2019-02-21 07:42

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.

查看更多
地球回转人心会变
3楼-- · 2019-02-21 07:43

Below code works for me.....

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

 private void Panel1_MouseWheel(object sender, MouseEventArgs e)
        {
         panel1.Focus();
         }
查看更多
Emotional °昔
4楼-- · 2019-02-21 07:43

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.

查看更多
登录 后发表回答