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

前端 未结 3 918
一整个雨季
一整个雨季 2021-01-11 18:30

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

相关标签:
3条回答
  • 2021-01-11 19:01

    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.

    0 讨论(0)
  • 2021-01-11 19:16

    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;
        }
    
    0 讨论(0)
  • 2021-01-11 19:24

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

    //Max width 200, unlimited height
    this.MaximumSize = new Size(200, int.MaxValue);
    
    0 讨论(0)
提交回复
热议问题