Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my form being resized when it is displayed?

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.

like image 881
adrianbanks Avatar asked Feb 14 '13 12:02

adrianbanks


2 Answers

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.

like image 133
John Alexiou Avatar answered Sep 18 '22 08:09

John Alexiou


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!)

like image 29
joe Avatar answered Sep 19 '22 08:09

joe