I have a form that appears as a modal dialog. The form looks like this in the designer:
(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:
(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.
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.
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With