I want to set limit scrolling to a scrollable panel

前端 未结 2 783
[愿得一人]
[愿得一人] 2021-01-21 22:37

I made a scrollable panel like this:

private void button3_Click(object sender, EventArgs e)
{
    Form f2 = new Form();
    f2.Size = new Size(400, 300);
    f2.         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 22:57

    Tweak this method: you need to "pin" the panel so it doesn't move below the top and above the bottom - because mouse wheel deltas are events you will continuously receive. You have to manually decide when to ignore them

    private void panel1_MouseWheel(object sender, MouseEventArgs e)
    {
            Form2 frm = new Form2();
            panel1.Top += e.Delta > 0 ? 10 : -10;
    
            // tweak this
            if (panel1.Top > 0) panel1.Top = 0;
            else if (panel1.Bottom <= panel1.Parent.Height) panel1.Bottom = panel1.Parent.Height;
    
            Console.WriteLine("panel2.top:" + panel1.Top);
    
    }
    

    Also, the above will work when the panel you are scrolling is "Taller" than the viewport (the form itself). You might need to tweak further when the panel is smaller than the form - so just test a few cases.

    You also need to pay attention to the Resize event so your panel has the correct Top property when someone expands the container form.

提交回复
热议问题