我犯了一個滾動面板是這樣的:我想設置限制滾動到一個滾動面板
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.Top <= panel1.Parent.Height)
panel1.Top = panel1.Parent.Height;
Console.WriteLine("panel2.top:" + panel1.Top);
}
這是面板,PANEL1 =泛的完整代碼...
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.Top <= panel1.Parent.Height)
panel1.Top = panel1.Parent.Height;
}
}
而且我不想比button1更高或低於最後一個按鈕。
你能發表完整的表格代碼嗎? – John
明白了。它與限制增量有關 - 發佈你回答 – John
這是因爲你忘記了鼠標滾輪事件不斷被鼠標觸發,所以當面板位於頂部或底部時,需要添加邏輯來忽略它們。 – John