我想设置限制滚动到一个滚动面板

问题描述:

我犯了一个滚动面板是这样的:我想设置限制滚动到一个滚动面板

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更高或低于最后一个按钮。

+0

你能发表完整的表格代码吗? – John

+0

明白了。它与限制增量有关 - 发布你回答 – John

+0

这是因为你忘记了鼠标滚轮事件不断被鼠标触发,所以当面板位于顶部或底部时,需要添加逻辑来忽略它们。 – John

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

ScrollBar.Maximum属性

一个例子如下。

以下示例假定您已经创建了一个窗体,向窗体添加了一个PictureBox,并向该PictureBox添加了一个水平HScrollBar和一个垂直VScrollBar。此代码示例是为ScrollBar类概述提供的更大示例的一部分。 在此示例中,Maximum属性设置为图像的大小加上滚动条的大小(如果它可见),再加上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 
     } 
    } 

希望你可以更改相应的代码来设置最大滚动空间:)

调整此方法。你需要“销”的面板,因此不会移动下面的顶部和高于底部 - 因为鼠标滚轮变化是您将不断收到的事件。您必须手动决定何时忽略他们

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属性。

+4

谢谢,应该工作,也...我有那mousedown和mousemove所以面板可以滚动拖动鼠标,但与panel1.Top = 0;当你尝试在面板上方滚动时看起来很奇怪..你还有其他什么吗? –

+4

另外...你不需要计算panel1.bottom吗? –

+4

这就是这样工作的...当我尝试在面板下面滚动时,它会再次上升......我希望它静止不动。 –