2012-01-16 60 views
8

我寫了一個函數來動態添加元素到「面板」。動態面板元素添加和滾動條

public int State;   
public Point Point = new Point(0, 0); 
public void DialogAdd(string message, string author) 
     { 
      var d = new DialogMessage(); 
      if(State == 0) 
      { 
       d.BackColor = Color.FromArgb(255, 237, 241, 245); 
       State = 1; 
      } 
      else 
      { 
       State = 0; 
      } 


      d.Controls["name"].Text = author; 
      d.Location = new Point(0, Point.Y); 
      d.Controls["msg"].Text = message; 
      Point.Y += d.Size.Height; 
      Controls["panel1"].Controls.Add(d); 

     } 

DialogMessage是UserControl,在所有組件上都具有屬性「AutoSize = true」。 此面板獲得了AutoScroll屬性,因此獲得了滾動條。 問題是元素是以不同的方式添加的,具體取決於滾動條的位置。如果滾動條位於頂部,則根據需要添加。

enter image description here

,但如果在底部添加了滾動條的時候,再加入項目腳麻

enter image description here

請告訴我什麼,我做錯了,如何解決它?謝謝。遺憾的英語不好

+0

能取悅添加信息和標籤什麼API您正在使用?我的猜測是Windows窗體,但如果你只是添加正確的標籤,你會覆蓋更多的人。 – dowhilefor 2012-01-16 19:48:46

+0

是的,它是Windows窗體。 – Anton 2012-01-16 19:53:48

+0

現在,我通過使用FlowLayoutPanel解決了這個問題,但我仍然想知道,我在做什麼錯誤:) – Anton 2012-01-16 20:08:00

回答

6

當放置在面板內部的控件,你必須補償滾動位置:

基本上,嘗試使用這一行:

d.Location = new Point(0, panel1.AutoScrollPosition.Y + Point.Y);