C# 调整 winforms 的大小不能正常工作

Mag*_*end 0 c# winforms

我正在使用MetroFramework开发一个 winforms 应用程序。我想以编程方式调整表单的大小,以便更大的对象可以放在上面。为此,我在对象中使用框架中的切换tabControl。使表单变大工作正常,但是当我禁用切换时,它不想缩小表单。

private void tSynced_CheckedChanged(object sender, EventArgs e)
{
    if (tSynced.Checked)
    {
        //Sync enabled
        Console.WriteLine("Sync enabled");
        this.Size = new Size(this.Width + 300, this.Height);
        this.MinimumSize = new Size(this.Width, this.Height);
        this.MaximumSize = new Size(this.Width + 200, this.Height);
    } else
    {
        //Sync disabled
        Console.WriteLine("Sync disabled");
        this.Size = new Size(this.Width - 300, this.Height);
        this.Width = 534;
        Console.WriteLine(this.Size);
        this.MinimumSize = new Size(this.Width, this.Height);
        this.MaximumSize = new Size(this.Width, this.Height);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我一直在尝试一些我知道的技术,只是为了尝试一下,然后从那里回来,但它似乎不起作用。这对我来说似乎很奇怪,因为第一种方法(this.Size行)在使表单变大时确实有效。该this关键字是指将要Form根据到Visual Studio。它似乎没有尝试调整 的大小tabControl,因为我将其绑定到右侧,并沿着右边框正确移动。

的日志this.Size将返回大值,这就是{Width=834, Height=354}我的情况。

我已经尝试将初始值保存在一个Size变量中,并从那里恢复它(因为它会考虑用户调整大小),但这似乎无法正常工作。

Size oldSize; //Global variable
private void initialize()
{
    oldSize = new Size(this.Width, this.Height);
    Console.WriteLine(oldSize);
}

this.Size = oldSize; //In the eventhandler
Run Code Online (Sandbox Code Playgroud)

日志将返回正确的值,就{Width=534, Height=354}我而言。但是它在this.Size再次设置属性时拒绝使用该值...

我在看什么?

stu*_*rtd 5

放大表单时,您将 MinimumSize 设置为当前大小 - 但是当您将其缩小时,您尝试在仍设置 MinimumSize 的同时重置大小 - 因此表单不会调整大小。

您需要做的就是在更改大小之前重置最小大小。