我有一个窗口表单,一面有控件列表,另一面有图表。我想要一个隐藏和显示图形的复选框,但也要缩小或增大表单以适应它。
我尝试使用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;
}
如何显示图表并根据需要调整表单大小,但不限制用户自己调整表单大小?
答案 0 :(得分:2)
我想出的解决方案是保存大小,禁用自动调整大小,然后强制调整大小:
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
,以便所需金额将在表格上显示。