Why does Visual Studio automatically changes the layout of my form?

16,173

Solution 1

Reasoning

This was due to AutoScaleMode property which scales the size and position of objects in winforms based on the difference in Dots per inch (DPI) between different development machines. This is a feature that cannot and should not be changed. For official documentation, see Automatic Scaling in Windows Forms on MSDN.

Let's say there are two machines, machine A and B. Machine A has 96 DPI and scaling factor 100%. Machine B has 144 DPI and 150% scaling factor as its recommended settings. A WinForm was created in machine A and machine B is trying to edit that WinForm.

When Visual Studio in machine B encounters that machine B's DPI is different than the one used to create the WinForm, it automatically scales the objects in the WinForm to try to make the WinForm appear in machine B as how it would look in machine A​​. Hence, this causes some properties in WinForm.Designer.cs file to be automatically changed.

Workaround

In order to continue editing WinForms in high DPI machines without any automatic alignments, we need to set the DPI in high DPI machines to match the DPI in the machines used to create the WinForm. This can be done by adjusting the scaling factor to 100% (96 DPI; right-click on desktop > Display settings) and restarting Visual Studio (or re-login to Windows, if that does not work).

NOTE: The key here is to adjust the DPI to the original DPI of the machines used to create the WinForm. Just adjusting the scaling factor might not work. You might need to play around with the screen resolution.

Solution 2

My solution to this was to add the registry key LogPixels and rebooting the machine. This key was previously not present on my machine.

[HKEY_CURRENT_USER\Control Panel\Desktop] "LogPixels"=dword:00000096 

I am on Windows 10 with a high resolution display. I tried to toggle my display settings back to 100% but it had no effect.

This registry setting worked for me.

Solution 3

The issue propably lies in Visual Studio 2010 or in the Windows 10 (depends how you look at it). Graphics API's were changed after Windows 7 for which VS 2010 was made. Then there was normal to have 100% DPI scalling and nothing else. Maybe you should try installing Visual Studio 2015 Community Edition which should fix the issue.

EDIT:

Currently there is no way to lock WinForms editor from changing the settings in the designer file. This is actually expected behaviour so the positioning/size/scaling values change to adjust the current display settings so the values can be adjusted if Autoscaling and DPI awarness are turned on. Here are some good resources for working with High DPI settings:

Solution 4

Having run into this problem myself recently I've spent some time following these responses and other resources elsewhere as well. I encountered this issue while developing an Excel Addin using the VSTO tools in VS 2015 Pro, under Windows 10.

After playing around quite a bit, I then created a one-form Windows Forms solution with which I could observe the effect of various things such as the AutoScale properties, etc. I've run into something which I think is truly strange:

Within the Excel Addin solution, I set a breakpoint at the first line of code in the form.Load handler. At that point, I see:

Screen.PrimaryScreen.Bounds   {X=0 Y=0 Width=5760 Height=3240}

This is interesting well beyond the screen resolution on my system, which is 3840x2160.

I close this solution and immediately open the single-form "test" solution, which has one form (windows form) which has only 2 labels which are filled in the Load event with Top/Left/Width/Height values and a button whose title does the same, for the button itself. No other code whatsoever. Within this solution, run on the same system, I see:

Screen.PrimaryScreen.Bounds   {X=0 Y=0 Width=1920 Height=1080}

Why would Windows / Visual Studio report different screen size, on the same machine, with absolutely no changes made to system settings of any sort?

Basic system settings:

Lenovo Yoga 920, Screen resolution = 3840 x 2160, Scaling = 200%

Again, no changes made at all between running the two different solutions within Visual Studio.

ANY hints or ideas will be most welcome!

On the whole, it seems to me that MS has really stuffed things up in terms of being able to use VS to create portable solutions! Just my 2c....

Solution 5

I had the same problem with a program I edited in another computer with other DPI settings. The workaround I found based on this tutorial on DPI was to locate the following lines on Designer.vb (working on vb net)

    Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

comment them out, change autoscale to None by adding :

    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None

and rebuild and rerun the program. The controls were back in place. Afterwards I set AutoScaleDimensions back to the original values and AutoScaleMode back to Font by uncommenting the code above and everything worked fine. WinForms implements DPI scaling by its own scaling mechanism, which calculates the scaling difference between the system that the form has been designed on and the system it is running on. (as described in the aforementioned tutorial). I see that the workaround above resets the scaling factors of the machine that created the form.

Share:
16,173
John Evans Solachuk
Author by

John Evans Solachuk

Updated on July 24, 2022

Comments

  • John Evans Solachuk
    John Evans Solachuk almost 2 years

    I have a Windows form which consists of 3 files:

    • AbcFrm.cs

    • AbcFrm.Designer.cs

    • AbcFrm.resx

    Whenever I do a little change to any properties of datagridview or add new events, the size of the controls grows larger to the right and bottom side and causes some part of the controls to go out of bounds.

    I have compared the .Designer.cs file before and after I perform a minor property change (e.g. change a datagridview's tabStop property value from true to false) and discovered some lines/properties were added/changed automatically in the .Designer.cs file :

    • some controls' .Size value changes
    • some controls' .Location value changes
    • some controls' .Padding value changes
    • the form's .AutoScaleDimensions becomes larger
    • the form's .ClientSize becomes larger

    Edit

    I realize when I change the resolution from 150% to 125% and reopen the application, the controls are back in the correct place due to the windows form is now having a larger scaling size. My problem is similar to this question

    The cause is that the code might have been edited in a computer with different resolution. However, the solution is not satisfying as it does not solves the problem, only identifying the cause.

    How do I prevent this from happening? I'm using Visual Studio 2010 and my OS is Windows 10.

  • Palec
    Palec about 7 years
    Changing scale factor worked for me on Windows 10 with Visual Studio 2013. I have a three-monitor setup where my notebook's display has high DPI and it is the primary display. I had to lower its scale factor to 100% even though my VS is running on another display.
  • John Evans Solachuk
    John Evans Solachuk almost 6 years
    Just changing the scaling factor might not work. You might need to play around with the screen resolution too. Hence, it will also be very helpful if you knew what resolution and scaling factor the form was originally created in or last saved in because by then you wouldn't need to do any guessing.