Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 232
John Evans Solachuk Avatar asked May 09 '16 07:05

John Evans Solachuk


People also ask

What is form layout window in Visual Studio?

Form Layout window is a simple visual basic design tool whose purpose is to give the user a thumbnail view of the current form. This helps in controlling the form position in the Windows environment.

How do I display a form in Visual Studio?

Click on the tab for the second form (the subForm) in your design and double click on the button control to display the Click event procedure. Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.

Which layout control can set the property of control at runtime?

The TableLayoutPanel control arranges its contents in a grid. Because the layout is done both at design time and run time, it can change dynamically as the application environment changes.


5 Answers

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.

like image 190
John Evans Solachuk Avatar answered Oct 01 '22 20:10

John Evans Solachuk


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:

  • WinForms Scaling at Large DPI Settings–Is It Even Possible?
  • When using the WinForms designer, do I always need to have DPI set to 96?
  • Cannot disable DPI scaling for Visual Studio 2012
  • Visual Studio extensions and DPI-awareness
like image 34
fsacer Avatar answered Oct 01 '22 21:10

fsacer


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.

like image 38
Ocean Airdrop Avatar answered Oct 01 '22 19:10

Ocean Airdrop


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

like image 25
Neil Cothran Avatar answered Oct 01 '22 20:10

Neil Cothran


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.

like image 45
Ananas1494 Avatar answered Oct 01 '22 20:10

Ananas1494