2012-01-16 61 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);