2016-12-04 100 views
2

我犯了一個滾動面板是這樣的:我想設置限制滾動到一個滾動面板

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

回答

0

我們可以獲取或設置可滾動範圍的值的上限與

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

希望你可以更改相應的代碼來設置最大滾動空間:)

2

調整此方法。你需要「銷」的面板,因此不會移動下面的頂部和高於底部 - 因爲鼠標滾輪變化是您將不斷收到的事件。您必須手動決定何時忽略他們

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

這就是這樣工作的...當我嘗試在面板下面滾動時,它會再次上升......我希望它靜止不動。 –