1

I'm developing in vb.net a Winform .NET 4.0 application with Visual Studio 2013 on Windows 10. Recently I buy a new laptop, and I use it with its maximum screen resolution of 2550x1440.

Visual Studio seems to automatically change file *.designer.vb and *.resx, for each form I modify from the new pc, changing location and anchor of some controls. To be more precise, when I re-open the Winform project on a another (old) pc, I find a real mess on: - the size of the form: in general the form size is decreased, hiding some controls; - the location of some controls: some of them are overlapped with others; - the ImageScaling property of ToolStripMenu icons is automatically changed from 16x16 (the correct one) to 24x24, causing really big menus!

I read a lot of pages over the internet on this topic, but I cannot find any suitable solution. Some of them are from StackOverflow, such as: - How do I keep Visual Studio's Windows Forms Designer from deleting controls? (cannot really understand why VS touches continuosly controls) - Why does Visual Studio automatically changes the layout of my form? - visual studio 2005 designer moves controls and resizes Form (regarding this, it is not a solution to use docking instead of anchoring, some controls do not support it) - How to write WinForms code that auto-scales to system font and dpi settings? (if I set the AutoScale property to DPI, the form displays not correctly, and cannot be resized properly!)

Other solutions I tried, but with no luck: - disabling the AutoSize property of the controls: not a solution, because if you do it for example on ToolStripMenus, you have to change their size manually each time you add a button or whatever; - act on AutoScaling property: I tried every combination of this property, the only one which works fine is "Font".

This is a REALLY annoying problem: is there a way to block VS from modifying locations and dimensions of controls? Possibly without re-ordering manually (or re-doing) the layout of each form? Thanks a lot to anyone

Community
  • 1
  • 1
  • I fixed it like this : dont use any anchor properties and only use normal panels. Use lots of normal panels and their Dock property to position your controls. Its a bit of work but in my case my form does not only survives another resolution but even looked reasonable ok Make a small test project to check if it works for you – GuidoG Aug 18 '16 at 15:10
  • Thanks, I'll try to do that. To anyone who downvoted my question, please explain why, I cannot understand that. Otherwise I cannot know how to improve the question, or if it is clear enough. – johnnyontheweb Aug 18 '16 at 22:31
  • I tried to use normal panels, docked. It works but it increase slightly the winform design time. In an application with a lot of forms, it is hard to modify all the previously made ones. I still think we need a solution to block VS from changing *.Designer.vb and *.resx files. – johnnyontheweb Aug 31 '16 at 10:21
  • yes it is too much work to convert an existing project that is true. It can help you with new projects and new forms though – GuidoG Aug 31 '16 at 10:47

1 Answers1

1

In the proccess of setting form dimensions, the form size is restricted by Screen.GetWorkingArea without taking into account AutoScale.

I resolve this issue storing the original ClientSize setted in InitializeComponent() and reset it autoscaled on HandleCreated event stage.

    private SizeF _autoScaleFactor;
    private Size _originalClientSize;

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

        _autoScaleFactor = AutoScaleFactor;
        _originalClientSize = new Size(x, y);
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        AutoScaleClientSize();

        base.OnHandleCreated(e);
    }

    private void AutoScaleClientSize()
    {
        var dx = _autoScaleFactor.Width;
        if (!dx.Equals(1.0F))
        {
            _originalClientSize.Width = (int)Math.Round(_originalClientSize.Width * dx);
        }

        var dy = _autoScaleFactor.Height;
        if (!dy.Equals(1.0F))
        {
            _originalClientSize.Height = (int)Math.Round(_originalClientSize.Height * dy);
        }

        ClientSize = _originalClientSize;
    }
Charlie
  • 1,265
  • 15
  • 18
  • That is not the OP's problem at all, he's got a big monitor. He's struggling with VS being a dpiAware app so the designer applies the AutoScaleMode at design time as well. That the layout changes don't always pan out well is a universal issue, nice that you see it go wrong in the designer instead of on the user's machine. True WYSIWYG. – Hans Passant Jul 22 '18 at 22:14
  • @HansPassant, I faced same problems. Beside attempt changes in Dock and Anchor properties I found out your answer in https://stackoverflow.com/a/16307839/848737 that let me solve the form size issue. Thanks for that. – Charlie Jul 23 '18 at 12:39