How to avoid unnecessary changes in *.designer.cs and *.resx?

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?


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

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.

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.

Need Your Help

What is the size of column of int(11) in mysql in bytes?

mysql types int

What is the size of column of int(11) in mysql in bytes?

symfony2 twig whitelist html tags

html templates escaping symfony twig

I pass a variable to my twig template in Symfony2, this variable may contain <br /> html tags, I have tried to create an extension (function), but the variable still gets escaped.