How can I scroll my panel using my mousewheel?

前端 未结 9 1550
情歌与酒
情歌与酒 2021-01-17 09:19

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?

相关标签:
9条回答
  • 2021-01-17 09:47

    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..

    0 讨论(0)
  • 2021-01-17 09:50

    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;
    }
    
    0 讨论(0)
  • 2021-01-17 09:51

    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.

    0 讨论(0)
  • 2021-01-17 09:52

    What worked for me was adding panel1_MouseEnter EventHandler:

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        panel1.Focus();
    }
    
    0 讨论(0)
  • 2021-01-17 09:52

    Below code works for me.....

        Public Form
    {
    InitializeComponent();  
    this.MouseWheel += new MouseEventHandler(Panel1_MouseWheel);
    }
    
     private void Panel1_MouseWheel(object sender, MouseEventArgs e)
            {
             panel1.Focus();
             }
    
    0 讨论(0)
  • 2021-01-17 09:53

    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.

    0 讨论(0)
提交回复
热议问题