【问题标题】:WinForm automatically resize window for new controls but still allow user to adjust the size?WinForm 为新控件自动调整窗口大小但仍允许用户调整大小?
【发布时间】:2017-06-23 17:34:55
【问题描述】:

我有一个 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; 
    }

如何显示图表并按需调整表单大小,但不限制用户自行调整表单大小?

【问题讨论】:

    标签: c# winforms controls autosize


    【解决方案1】:

    我想出的解决方案是保存大小,禁用自动调整大小,然后强制调整大小:

        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,以便获得所需的数量在表格上可见。

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2017-03-12
      • 2013-09-19
      • 1970-01-01
      相关资源
      最近更新 更多