我想将限制滚动设置为可滚动面板

我做了一个像这样的可滚动面板:

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  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 = pan …

 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 += eY - pPt.Y; if (panel1.Top > 0) panel1.Top = 0; else if (panel1.Top <= panel1.Parent.Height) panel1.Top = panel1.Parent.Height; } } 

你可以用鼠标拖动面板滚动它,但问题是它看起来像这样:

我想不要高于button1或低于最后一个按钮。

调整此方法:您需要“固定”面板,使其不会移动到顶部下方和底部上方 – 因为鼠标滚轮增量是您将不断接收的事件。 您必须手动决定何时忽略它们

 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); } 

此外,当您滚动的面板比视口(表单本身)更“高”时,上述操作将起作用。 当面板小于表格时,您可能需要进一步调整 - 所以只需测试几个案例。

您还需要注意Resize事件,以便在有人展开容器表单时,您的面板具有正确的Top属性。

我们可以使用获取或设置可滚动范围的值的上限

ScrollBar.Maximum属性

一个例子如下。

以下示例假定您已创建表单,向表单添加PictureBox,并向PictureBox添加水平HScrollBar和垂直VScrollBar。 此代码示例是为ScrollBar类概述提供的更大示例的一部分。 在此示例中,Maximum属性设置为Image的大小加上滚动条的大小(如果可见)加上LargeChange属性大小的调整因子。 您必须添加对System.Drawing和System.Windows.Forms命名空间的引用才能运行此示例。

 public void SetScrollBarValues() { //Set the following scrollbar properties: //Minimum: Set to 0 //SmallChange and LargeChange: Per UI guidelines, these must be set // relative to the size of the view that the user sees, not to // the total size including the unseen part. In this example, // these must be set relative to the picture box, not to the image. //Maximum: Calculate in steps: //Step 1: The maximum to scroll is the size of the unseen part. //Step 2: Add the size of visible scrollbars if necessary. //Step 3: Add an adjustment factor of ScrollBar.LargeChange. //Configure the horizontal scrollbar //--------------------------------------------- if (this.hScrollBar1.Visible) { this.hScrollBar1.Minimum = 0; this.hScrollBar1.SmallChange = this.pictureBox1.Width / 20; this.hScrollBar1.LargeChange = this.pictureBox1.Width / 10; this.hScrollBar1.Maximum = this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width; //step 1 if (this.vScrollBar1.Visible) //step 2 { this.hScrollBar1.Maximum += this.vScrollBar1.Width; } this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange; //step 3 } //Configure the vertical scrollbar //--------------------------------------------- if (this.vScrollBar1.Visible) { this.vScrollBar1.Minimum = 0; this.vScrollBar1.SmallChange = this.pictureBox1.Height / 20; this.vScrollBar1.LargeChange = this.pictureBox1.Height / 10; this.vScrollBar1.Maximum = this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height; //step 1 if (this.hScrollBar1.Visible) //step 2 { this.vScrollBar1.Maximum += this.hScrollBar1.Height; } this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange; //step 3 } } 

希望您可以相应地更改代码以设置最大可滚动空间。:)