![](/img/trans.png)
[英]Set the vertical scroll bar to a particular position when autoscroll is true
[英]Control position changes when vertical scroll bar is displayed
一旦顯示面板的垂直滾動條,控件的位置就會改變。 啟用了Panel
中的AutoScroll
屬性
碼:
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);
}
當滾動條顯示在此處時,控件MsgItem
的位置太遠了,這是一個示例,它看起來像這樣。
使用FlowLayoutPanel和而不是Panel,而不必設置Position
你可以這樣
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;
}
}
因此,對於使用TableLayoutPanel
或FlowLayoutPanel
布局方案,建議使用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.