I am facing an issue where a WinForms form is being automatically re-sized every time the designer is opened.
This only appears to happen with a certain setup, however it can easily be replicated with the following steps...
FormBorderStyle
property to FixedSingle
ShowIcon
property to false
ControlBox
property to false
The problem I have with this is that when it happens it does not resize any of the controls (i.e. the ones set with anchors), so this means I end up with controls that overlap the form edge and everything needs to be manually readjusted every time I open the designer which is a pain.
So the question is: Why is this happening, and what can I do to stop it happening?
I am currently using Visual Studio 2012 Professional, and John Willemse has confirmed via comments that this issue is also present in Visual Studio 2010 Professional.
I see it, this should be a bug in any VS version. It is caused by the ShowIcon property, the designer doesn't handle it correctly when you set it to False. At issue is a bit of code in the Form class that looks like this:
FormBorderStyle borderStyle = FormBorderStyle;
if (!ShowIcon &&
(borderStyle == FormBorderStyle.Sizable ||
borderStyle == FormBorderStyle.Fixed3D ||
borderStyle == FormBorderStyle.FixedSingle))
{
cp.ExStyle |= NativeMethods.WS_EX_DLGMODALFRAME;
}
In other words, when ShowIcon is False then it uses a different border style from WS_BORDER, it uses the one of a modal dialog. Which has different borders on older Windows versions, they are fatter. Not exactly sure what inspired this code, probably has something to do with Windows 98.
Problem is, the Size property is a calculated value, the Winforms designer only stores the ClientSize property. So when ShowIcon is False then it should redo this calculation, it doesn't.
You can report the bug at connect.microsoft.com but the odds that Microsoft is going to fix it are exceedingly low so it would probably be a waste of your time. There is a very simple workaround for it, instead of setting ShowIcon to False in the Properties window, do it in the constructor instead. Like this:
public Form1() {
InitializeComponent();
this.ShowIcon = false;
}
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