我写了一个函数来动态添加元素到“面板”。
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 属性,因此具有滚动条。 问题在于元素的添加方式不同,具体取决于滚动条的位置。如果滚动条在顶部,则根据需要全部添加。
但是如果在底部添加滚动条的时候,那么添加项就出错了
请告诉我我做错了什么以及如何解决?谢谢你。对不起英语不好
最佳答案
将控件放在面板内时,必须补偿滚动位置:
基本上,尝试使用这一行:
d.Location = new Point(0, panel1.AutoScrollPosition.Y + Point.Y);
关于c# - 动态面板元素添加和滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8885342/