19

When i open a form in visual studio 2005 (c#) the designer automaticaly resize the form and move/resize controls without touching the designer at all. The source file is changed and when i close the designer i'm asked to save the *.cs file. I tried to look into visual studio options without any success. any ideas? visual studio setup or something? thanks, Tal

10 Answers 10

10

I have been working on this problem for most of today and found some interesting things: The main source of the problem seems to be relying on anchoring. If I use docking to position my controls, instead of anchoring, my problems seem to go away. I found a couple of blog posts from 2003(!), which detail how you might use docking instead of anchoring, and explain how anchoring can break the Windows Forms designer. It seems like this problem might be over 7 years old!

Here are the posts:

1
  • 1
    Controls in my form were consistently being skewed by the designer - the problem still exists in VS 2012. Switching to docking seems to have stopped it.
    – pettys
    Dec 4, 2012 at 21:44
9

This is due to AutoScaleMode-property. Your forms have probably been designed with a different DPI or Font settings than you have now in Windows display settings. AutoScaleMode-property has 4 different possible values : Dpi, Font, Inherit or None. In Dpi or Font mode, your forms and controls will be automatically resized depending on windows display settings.

So, set the AutoScaleMode-property to None in all your forms and controls and they won't be automatically resized anymore. Try to design your forms in order to let sufficient space in every controls so that text will fit even if text size is set to 125%.

1
  • Seems to have done it for me. I had to edit the *Form.Designer.cs to change this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; , and remove this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); (both lines are together in the .Designer.cs file)
    – Jimmy
    Feb 3, 2017 at 15:10
4

I found a work around.

not sure what happens behind but i changed my display properties. and it works fine. here is the sequence: display propertis->settings tab->advance. in the the advance dialog i changed the "DPI Settings" from Large (120dpi) to Normal (96 dpi)

2
  • 4
    that's awful that the IDE designer is affected by your screen DPI. Sorry to hear that. :( Jul 21, 2009 at 13:50
  • 3
    Of the many horrors perpetuated by Windows, 'DPI settings' is one of the worst. This setting causes more problems with more applications than almost anything else that I have seen.
    – Stewbob
    Jul 21, 2009 at 20:46
3

Had the same problem with controls anchored top, left and right within complex TabControls. The visual studio forms designer was increasing the width of all nested controls each time I would open the form.

I found a simple workaround thanks to this post. I simply added a panel to each tab and set their dock property to fill. All existing controls within the tabs were moved inside those panels. This works, even if the controls are anchored top, left and right.

Works at least for Visual Studio 2013 and 2015.

1
  • 1
    The same problem is still present in Visual Studio 2019. The workaround of panel-in-each-tab also still works around it. It seemed to be exacerbated in my case by having a TabControl as the primary UX with each tab having an instance of a UserControl with nested UserControls. The designer would resize labels on other tabs completely unrelated to where the UserControl lived.
    – amonroejj
    Dec 21, 2022 at 22:12
1

This is one you should live with. Even in VS2008 such things happen from time to time. It is mostly depends on form content (controls, positions, etc), and there is no option in VS to disable such behavior.

When you open your form in designer, vs runtime rebuilds visual appearance from code behind. And sometimes it made changes at this moment. Also when you are simply adding one control to form, designer fully rebuilds codebehind and resource files. This is well known issue, and seems that MS won't fix it, because they move in WPF direction.

So several points to simplify your life:

  1. Move to VS2008, designer were more consistent, but still shuffle controls in .designer.cs file
  2. Place your code in one of the source repositories, so if you accidentally saved such form, you can restore it from repositary.
3
  • thanks arbiter. -well, moving to 2008 isn't relevant for my project (involves too many code at this time).
    – tal
    Jul 21, 2009 at 13:48
  • VS2005->2008 migration is not problematic at all. Jul 21, 2009 at 14:17
  • yes, i have issues in vs2010 - but as RyanTM suggests, my troubles gone away, since i exchanged anchoring with docking ....
    – womd
    Oct 3, 2011 at 14:32
0

Setting the form Min and Max size settings to the current size was a good work around for me. This prevented VS from resizing it.

0

I had a trivial form with few controls on it, where the OK and Cancel at the bottom were being shifted up as soon as the form was opened in the VS2013 designer. The same behaviour was observed in VS2015.

The accepted answer here of DPI did not solve the issue for me, nor were there any issues on the size of the form/padding/margins.

Removing the controls that are shifted and adding them back into the form solved the problem for me, as suggested by ptutt here: Visual Studio designer moving controls and adding grid columns when form is opened

While I appreciate ryantum's suggestion and links of using docking [with panels], as also referred to in the link above with Roland's blog post here https://weblogs.asp.net/rweigelt/28984, with something so trivial I'd rather just make it go away with removing/adding back in.

0

I found locking the controls from the format menu was a simple and effective solution. VS2013

-1

I had this problem with VS 2015. I used dock panels with the controls that moved unexpectedly as their childs. By default the controls will be aligned to the left, but you can change the orientation. My buttons stopped moving.

-2

I had this issue, too. Every time I opened the designer, every box with anchor "right" was moved about 20 Pixels to the left. Additionally, the bottom of every box with anchor "left" was about 200 pixels outside the form.

This form has many controls and should not shrink on smaller displays, so it was set to autoscroll, the form itself was smaller in the designer than the shown minimum size (historically...). I just set the size to the minimum size so that no scroll bars appeared in the designer and the anchors worked as expected without screwing up the postitions.

I read the first post from ryantm's answer which led me to the solution. Apparently it has something to do with the order .Net executes events such as setting the size of a form.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.