C# 是否可以设置表单的最大宽度但不限制最大高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13527142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:54:15  来源:igfitidea点击:

Is it possible to set the Maximum Width for a Form but leave the Maximum Height Unrestricted?

c#winforms

提问by Paul Matthews

For some reason if you set both of the width and the height of Form.MaximumSize to zero it will let you have an unrestricted window size, however if you want to set a limit you have to do it for both width and height at the same time. I want a fixed width but no limit on height.

出于某种原因,如果您将 Form.MaximumSize 的宽度和高度都设置为零,它将让您拥有不受限制的窗口大小,但是如果您想设置一个限制,您必须同时对宽度和高度进行设置时间。我想要一个固定的宽度但没有高度限制。

    // No Limits
    this.MaximumSize = new Size(0,0);

    // Form Height will be stuck at 0
    int ArbitraryWidth = 200;
    this.MaximumSize = new Size(ArbitraryWidth, 0);

回答by Serge Belov

Is it possible to set the Maximum Width for a Form but leave the Maximum Height Unrestricted?

是否可以设置表单的最大宽度但不限制最大高度?

Not really. You could simulate it as follows:

并不真地。你可以模拟如下:

    private void Form1_Resize(object sender, EventArgs e)
    {
        SetMaximumWidth();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SetMaximumWidth();
    }

    private void SetMaximumWidth()
    {
        if (Width > 200)
            Width = 200;
    }

回答by Dan Bechard

Use INT_MAX since that's the theoretical limit that can be represented by a Sizeanyway:

使用 INT_MAX 因为这是可以用 a 表示的理论限制Size

//Max width 200, unlimited height
this.MaximumSize = new Size(200, int.MaxValue);

回答by wakaka

I know the question is a couple of years old but I met the same problem and what I did was to set one of the parameter to the resolution of the screen, assuming the size of the form cannot exceed the size of the screen.

我知道这个问题已经有几年了,但我遇到了同样的问题,我所做的是将其中一个参数设置为屏幕的分辨率,假设表单的大小不能超过屏幕的大小。

this.MaximumSize = new Size(200, Screen.PrimaryScreen.Bounds.Height);

Hopes this helps someone.

希望这有助于某人。