23

For some reason Visual Studio winforms designer time to time applies dull changes to form designer files and respective resx.

Most annoying so far:

  • changes to order of controls declarations/initialization
  • changes to some control sizes (most notably menu items widths)
  • changes to serialized images embedded into resources (...wait, what???)

Those changes doesn't affect form/user control functionality or it look, but they create lot of noise in source control, making merges almost impossible, or require error prone manual fixing to eliminate all changes that actually change noting, just until next change to designer.

Any ideas how to prevent studio from it?

Andriy K
  • 3,302
  • 31
  • 42
  • I'd be happy to do that, but there are cases when forgetting something is not an option. E.g. when you have huge legacy codebase you have to somehow coexist with. – Andriy K Jul 08 '14 at 16:46
  • 2
    It is too poorly documented to have a shot at being accurate. I'll randomly guess that this form was created on a machine with a different video adapter resolution. Compare the AutoScaleDimensions assignment in InitializeComponent() against one that you create from scratch. – Hans Passant Jul 08 '14 at 18:07
  • Thank you, Hans. Good point. As our forms passed weren't actually created, but generated by third party utility, they had no AutoScaleDimensions set at all. Will try to set it and see the consequences. – Andriy K Jul 10 '14 at 09:19
  • Ugh, pretty criminal to leave that detail out of the question. Add not properly implementing the ISupportInitialize contract to the list of possible reasons. – Hans Passant Jul 10 '14 at 09:26
  • Agree, but they were kind enough to generate Cursor = Default and BackColor = Control all around. It looks like adding AutoScaleDimensions and AutoScaleMode didn't help. On my machine reopening just saved form and saving it again caused resize of context menu strip and plenty of changes to serialized images in resources. On my teammate's machine it also changed all the menu item sizes. Strange, but new form created on his machine contained no AutoScaleDimensions, only AutoScaleMode set to Font (no font though). Settings of DPI are same on both machines. – Andriy K Jul 10 '14 at 11:01
  • 2
    Is the "Lock controls" menu preventis this? Right click on the usercontrol/form, then select "Lock Control" to give it a try. – Larry Jul 18 '14 at 14:34
  • I would bet *anything* that your third party control is Infragistics. – Larry Jul 18 '14 at 14:35
  • 1
    Just curious but do you have multiple people working a single form at the same time? It sounds like since your issue is more with source control noise than with the shuffling itself then perhaps one possible solution is to approach it from that angle. Maybe only allow single user check out on certain items and then take advantage of things like partials and MetadataTypeAttribute if you cannot solve it in visual studio. I've had similar issues with Infragistics and forms inheriting from things. – Mike Cheel Jul 18 '14 at 14:35
  • 1
    LOL @Larry, see my comment – Mike Cheel Jul 18 '14 at 14:37
  • 1
    @MikeCheel :) I sincerely bow in front of your patience. I feel the sudden urge to add the *Infragistics* tag to this question. – Larry Jul 18 '14 at 14:46
  • Sorry guys, but there are no Infragistics in my project, it's ComponentOne :) On the reason why do we have to work simultaneously on form: we've got "god object" in our project that is single winforms class which originally had 17 KLOC, and somewhere around 20 KLOC accessing it as singleton instance from all around solution. We're trying to refactor this out, and, as you can imagine, this is task hardly done by single person. – Andriy K Jul 22 '14 at 14:02
  • I'm the only dev that working on this form and there is no 3rd party components in it and still once in a while designer changes order of components during save. it makes pushing it to source control longer than designing the form! – AaA Jan 16 '18 at 02:04
  • Its now 2020, VS 2019 still does the same. Enough to open a form in a VB.NET project which is under source control, VS will check it out, when saved (for example you change the code behind it but not the layout) its the same logical content in the .resx and .designer.vb files but most lines rearranged. Makes no sense. – Hefaistos68 Oct 30 '20 at 09:59

2 Answers2

12

Please try to have your control locked using the right button, then select "Lock controls" on your Form/UserControl.

enter image description here

Unfortunately, there is no way to separate "useful" changes in generated code from "pollution" that come from a control designer bug or internal working.

If the lock control feature does not work for whatever reasons, there is not much that we can do, except those horrible hacks:

  • Once your form is created and finished, move your generated code (InitializeComponent method and declarations) out of the .designer.cs. Visual Studio will not touch it anymore. The drawback is that you will no longer have visual support.

  • Having the .designer.cs file flagged as read only will prevent Visual Studio attempting to change it. It might cause Visual Studio control designer hiccups sometimes, but it will leave your .designer.cs intact.

There is another way that consist in not using the designer at all. This leads to a new way to embrace winforms programming, because you will provide the code to initialize controls with their properties and events.

It is not as huge and ugly as it seems to be: using reusable patterns and OO features could be very pleasant, and some well aligned methods calls are better that some horrible code hidden in a .designer.cs file.

Of course there will be no visual support, but you can cheat it by adding your control to an empty container in design mode to see how it looks like.

Larry
  • 17,605
  • 9
  • 77
  • 106
-2

As far as I understand, you have edited .designer.cs file. From one point it is OK, I guess we all do that. However, there is clearly notification there 'file is auto-generated, do not change it'. So Visual Studio puts you a warning that if you edit it things may not go always well. It is like that by design. And while those auto-changes might not make trouble to your app, they do sometimes (e.g. setting width and height to the control that should change its width and height contextually, and that is always necessary if you have multi-language app).

So if you don't like garbage, you don't use designer, because it creates garbage, that's how it works. Or you start with it and at some point decide to never open it again and then do your cleaning.

Or you go WPF, it meets your requirements, and it is about time to make WinForms legacy, at some point support for it will surely end.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57