Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designer automatically re-sizes form when certain properties are set

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...

  1. Create a new project in visual studio
  2. The default form size is 300 x 300, but whatever you set it to make a note
  3. Set the FormBorderStyle property to FixedSingle
  4. Set the ShowIcon property to false
  5. Set the ControlBox property to false
  6. Save the changes
  7. Close the designer
  8. Re-open the designer and you will notice the form has shrunk by 4 pixels (both width and height)

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.

like image 243
musefan Avatar asked Feb 13 '23 17:02

musefan


1 Answers

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;
    }
like image 102
Hans Passant Avatar answered May 20 '23 14:05

Hans Passant