[英]WinForm automatically resize window for new controls but still allow user to adjust the size?
我有一個Windows窗體,在一側有一個控件列表,在另一側有一個圖形。 我希望有一個復選框可以隱藏和顯示圖形,但也可以縮小或增大表單以容納它。
我嘗試對表單使用AutoSize=true
,但是用戶無法調整表單的大小(即,將圖形展開或縮小到他們的屏幕)。
然后我嘗試
private void toggleCheckBox_Click(object sender, EventArgs e)
{
theGraph.Visible = toggleCheckBox.Checked;
// automatically resize the form
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.OnResize(e);
// this will force the form back to its original size
// but without it the user cant adjust the form size
this.AutoSize = false;
}
如何顯示圖形並按需調整表單大小,但又不限制用戶自行調整表單大小?
我想出的解決方案是保存大小,禁用自動大小,然后強制大小:
private void toggleCheckBox_Click(object sender, EventArgs e)
{
theGraph.Visible = toggleCheckBox.Checked;
// automatically resize the form
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.OnResize(e);
var NewSize = new System.Drawing.Size(this.Width, this.Height);
// this will force the form back to its original size
// allowing the user to adjust the form
this.AutoSize = false;
// force the form back to its new size
this.Size = newSize;
}
注意:為了使AutoSize
與錨定控件一起正常工作,請確保為要切換的控件設置MinimumSize
,以便在窗體上可見所需的數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.