gpt4 book ai didi

c# - 动态面板元素添加和滚动条

转载 作者:太空狗 更新时间:2023-10-29 17:51:04 26 4
gpt4 key购买 nike

我写了一个函数来动态添加元素到“面板”。

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

请告诉我我做错了什么以及如何解决?谢谢你。对不起英语不好

最佳答案

将控件放在面板内时,必须补偿滚动位置:

基本上,尝试使用这一行:

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

关于c# - 动态面板元素添加和滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8885342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com