WinForm 自动调整新控件的大小 window 但仍允许用户调整大小?

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对于正在切换的控件,以便在表单上可以看到所需的数量。