2 回答
TA贡献1843条经验 获得超7个赞
你可以这样做
var count = panel2.Controls.Count;
MsgItem item = new MsgItem("testing");
item.Top = count == 0 ? 10 : (panel2.Controls[count - 1].Bottom + 10);
panel2.Controls.Add(item);
对于您的原始代码,即
int an = 10;
private void Incoming_Click(object sender, EventArgs e)
{
MsgItem item = new MsgItem("testing");
item.Top = an;
an = item.Top + item.Height + 10;
panel2.Controls.Add(item);
}
解决方案是跟踪面板的滚动
private void panel2_Scroll(object sender, ScrollEventArgs e)
{
switch (e.ScrollOrientation)
{
case ScrollOrientation.VerticalScroll:
var change = e.NewValue - e.OldValue;
an -= change;
break;
}
}
因此,对于布局场景,建议使用TableLayoutPanelorFlowLayoutPanel
- 2 回答
- 0 关注
- 142 浏览
添加回答
举报