Visual Studio Form Designer redesigned when reopened due to MinimumSize

So, I am creating a form layout and trying to set a minimum size for it to find out that WinForms has a fit! So, this is what it looks like when I start with the MinimumSize property set ONLY for width ...

enter image description here

As you can see, it looks great, without extra spaces, the form is the size I want, etc. .... Then I try to close the form and open it back, I have a little oddity (MinimumSize height was set equal to 0, and now WinForms made it 38), but everything that it worked as expected.

enter image description here

It's nice that the odd value of โ€œ38โ€ in MinimumSize-Height is strange, but whatever, let me try to set my preferred MinimumSize-Height to 420 (same size as the shape).

enter image description here

Save, close the form designer and open ... WTF? Everything went wrong! My three buttons below were pushed upward, so the text box and the TreeView control that I have above it, as well as the height of the left list, are reduced. In fact, looking at him, it seems that all the controls are still correct. The changed variable is the size of the form. It somehow went from 420 to 442.

enter image description here

Does anyone know why this is happening? What can I do to fix this, or do I think I get around this? Keep in mind that I have buttons, and the text box is pinned to the bottom. Tree formation should remain attached to the top and grow with the size of the window.

Thanks!

+5
source share
1 answer

This is a designer mistake, of course. This is caused by a feature of Winforms, which usually causes problems for the designer; in fact, it never stores the Size property. Something you can see in the auto-generated code in the Designer.cs file. Instead, it saves ClientSize and calculates the size based on this value. A very necessary feature, the height of the title bar is unpredictable, it depends on the configuration of the user.

What causes the error in your case is setting the ControlBox property to False. This also disables the icon, and this leads to the calculation of the external size. Something to do with the order in which the properties are assigned, I think the Form class should implement ISupportInitialize, but that is not the case. A value of 38 for MinimumSize.Height is a side effect of that header height on your computer. You cannot make the window smaller. The restriction, which also applies during development, is another quirk.

The workaround is simple. Set the ControlBox property back to True and just set its value to your form constructor:

  public Form1() { InitializeComponent(); this.ControlBox = false; } 
+5
source

Source: https://habr.com/ru/post/1209106/


All Articles