可滚动面板无法正常工作

时间:2016-12-05 08:35:31

标签: c# winforms

我有一个带有此代码的可滚动面板:

private void button3_Click(object sender, EventArgs e)
{
    Form f2 = new Form();
    f2.Size = new Size(400, 300);
    f2.AutoScroll = false;
    Panel pan = new Panel();
    pan.Size = new Size(600, 100);
    pan.AutoScroll = false;
    for (int i = 1; i <= 10; i++)
    {
        Button b = new Button();
        b.Text = "B" + (i);
        b.Name = "button_" + (i);
        b.Left = (b.Width + 12) * (i - 1);
        b.Parent = pan;
        pan.Parent = f2;
        f2.Show();
    }
}

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

public void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Console.WriteLine("panel2.top:" + panel1.Top);
    if (e.Button.HasFlag(MouseButtons.Left))
    {
        Form2 frm = new Form2();
        panel1.Top += e.Y - pPt.Y;
        if (panel1.Top > 0) 
            panel1.Top = 0;
        else if (panel1.Bottom <= panel1.Parent.Height) 
            panel1.Top = panel1.Parent.Height;
    }
}

当我尝试向上滚动时,我希望它能保持静止状态(适用于该代码),但当我尝试滚动得太低时,它会让我回到顶部,我希望它能够在那里静止...

0 个答案:

没有答案