Why is my form being resized when it is displayed?
Asked Answered
B

5

9

I have a form that appears as a modal dialog. The form looks like this in the designer:

as in the design view in Visual Studio (width = 360, height = 215)

When it is shown in the application, it gets 10 pixels taller and wider than is defined, leaving a wide margin around the bottom and left edges:

as in the running application (width = 370, height = 225)

The form is explicitly set to be 360x215 pixels in dimension, has a border style of FixedDialog, inherits from System.Windows.Forms.Form, and has no code in it to manipulate the dimensions (with the exception of the auto-generated designer file). If I change the border style to FixedSingle or FixedToolWindow it appears the correct size (but I want it styled as FixedDialog).

Any idea what is causing this?


I've fixed this by removing the MinimumSize setting on the form. It appears that if it is set to the same size (or near, but I haven't quite found the threshold yet) as the Size property, the margins are introduced. As the form is not resizable, I don't need the MinimumSize set so it can be removed.

I still don't understand why this is the case though.

Beneficent answered 14/2, 2013 at 12:54 Comment(14)
Have the max or min properties been set?Lavoisier
@CR41G14: yes, Size, MinimumSize and MaximumSize are all set to 360, 215.Beneficent
Out of curiosity, is it always being bumped up to 370x225 (no matter what starting dimensions you use), or is it instead always getting 10 pixels added to each side? Did you try other window sizes just to see the results?Acknowledgment
@jszigeti: It always adds 10 pixels, no matter what the starting size.Beneficent
Maybe it will help with some code. Did the propery margin appears somewhere in your code?Heartbeat
Well, I can report that something similar happens to me when I switch to FixedDialog. It increases the window by 8 pixels instead of 10. Unfortunately, I don't know why this happens, but I'm curious if it's just a disconnect in how VisualStudio shows you the form in the designer, vs. how Windows 7 draws it on screen. The border size, for example, increases from one to the other.Acknowledgment
The difference in size between your design form and at run time may come as a result of the AutoscaleMode property. What's it set at on the form (it can be one of Font, Dpi, Inherit or None)? Does it still happen when you set AutoscaleMode to None?Unswerving
Well, odd, this must be something environmental. You can never count on getting a fixed Size, the value is subject to theme customizations like the title bar font size. Winforms only tries to preserve the ClientSize. The only thing I can think of is the DpiAware manifest property, that might not match between the designer and your program.Lapointe
@Anthill: AutoScaleMode is set to Inherit.Beneficent
Have you tried setting to Austoscale None - if so what's the result?Unswerving
Does the SystemParameters class (msdn.microsoft.com/en-us/library/…)? I think that the size settings only define the client area, and in this area you can place your controls and it is guaranteed that its size will not change. The border sizes add to this client area and differ, depending on what kind of border you have. Otherwise - if the windows size would stay the same - different borders would enlarge (not so bad) or reduce (bad) your client area.Bathsheeb
Having the same issue since we upgraded a legacy project to target .NET Framework 4.5. Suddenly all our modal forms were 10 pixels smaller in both dimensions which led to some weird layout issues. Turns out pre 4.5 the forms were a little bit larger at runtime (+10 pixels in each dimension). After we targetted 4.5 they were the size we'd expect them to be. Somehow seems to be related to Windows Aero ... if I revert to the classic theme it doesn't enlarge the forms.Rental
Hans Passant's answer here explains what was happening in my case: #15982528Rental
Salam. This answer worked for me: https://mcmap.net/q/695247/-my-windows-form-keeps-on-shrinking-resizing-on-buildPleat
L
9

First, your form seems to have AutoScaleMode set to Font. This causes a form resize depending on the used font.

Second, ensure to have the following lines before creating the main form:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);  // Not using this - or using true - will cause a different font rendering
...
Application.Run(new Form1());                          // this creates your main form

(Usually this is part of the static Main method in Program.cs)

Not using those lines causes the usage of a different font rendering (have a look at your screenshots - the letters don't look exacly identical!)

Leucotomy answered 30/4, 2013 at 20:31 Comment(1)
Thanks, this helped. Setting AutoScaleMode to "dpi" fixed my issue (it was way larger than in design).Alphonse
F
2

I had similar problem with my form. Finally I found out that the problem was with the Maximum and Minimum Size of the form in properties. If you want a fixed, unresizable form, you have to disable them.

Hope this helps.

Frierson answered 17/2, 2013 at 7:20 Comment(0)
A
2

Why don't you brute force the issue with the code:

protected override void SetClientSizeCore(int x, int y)
{
    base.SetClientSizeCore(360, 215);
}

which sets the client area. You need to calculate what values you want.

Allianora answered 17/2, 2013 at 7:27 Comment(0)
M
2

I have created one form, changed the font and set the FormBorderStyle to FixedDialog, but I did not face any issue. Just have a check that what exactly are you setting on form load.

// this needs to be set as joe said is correct.
Application.SetCompatibleTextRenderingDefault(false);

If you can provide the code which you writing on formLoad method. So that I can try to rectify the problem.

Marabou answered 24/5, 2013 at 13:21 Comment(0)
R
0

I had some success by going into form "Form1.Designer.cs". Then I made sure the last word of the following code is "Normal":

    this.WindowState = System.Windows.Forms.FormWindowState.Normal;
Revision answered 3/10, 2021 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.