I'm aware, that the .designer.cs
file contains data generated by the visual form designer in Visual Studio. However, I have some additional methods though, which I want to put into the .designer.cs
file as well, because these are responsible for lower-level form handling (for example, pieces of my visual state manager).
The InitializeComponent
method inside the .designer.cs
file has a comment stating, that it is automatically generated and should not be modified by user. Does this restriction apply only to that method or shouldn't the .designer.cs
file be edited by user at all? I've noticed, that among others, it contains the Dispose()
method, which the user might want to modify - what suggests the first option. I want to be sure, though.
I think the other answers are simplifying too much.
First of all, I totally agree that it's almost always a bad idea to edit a .designer file, but there are a few cases where I've done so, feel it was good and proper, and didn't get burned.
Say I create a label and accidentally double click. Designer creates a method in my main .cs file which I then delete:
Well, now the code won't build unless I also delete the following from my .designer file:
Less frequently, the order in which things are added to a form or panel (or menu!) matters, and it can be easier to change this order in the code than in the Designer GUI. In my experience VS 2010 always picks up on this, updates its GUI's info, and redraws its preview. Just remember to focus on the
Add()
methods--the order variables are declared in generally doesn't matter.Ditto if you set a property that causes a line to be added to the .designer file, deleting the line gets picked up quickly and Designer refreshes. Maybe it's wiser/safer to use the GUI to change the property, but I think deleting the line is cleaner.
Code that is not inside this region,
#region Windows Form Designer generated code
, will only get generated once. It is safe to move, and as others have recommended elsewhere (https://stackoverflow.com/a/6527072/1593924), moving theDispose(bool)
method out actually can make a lot of sense, if you're modifying it or adding aDispose()
method that should ideally sit next toDispose(bool)
.DISCLAIMERS:
That said, I've only tried VS 2010 Ultimate; your mileage may vary on 1-3 above, but 4 should be safe as long as the .designer is a partial class with
Dispose(bool)
outside that#region
. I also make sure the latest good version of a .designer file is committed into the source repository before messing with it.By admitting to having gone along with the
Dispose(bool disposing)
pattern, I'm not meaning to promote that approach. There seem to be good reasons to simply useDispose()
in most cases, and only do more for unmanaged resources, each of which is encapsulated one-to-one in a dedicated Disposable object.Partial designer form class it's used by Visual Studio for placing all code need for build the control.
The method InitializeComponent() can't be overwrite: it's used by designer editor for render a preview of your form! Try in a new project: resize your form, add a label and a button and rename the InitializeComponent() method + re-compile. Your form back to default size!
If you need to call code by form loading, just override OnLoad() virtual method, if you need to call code by form showing, simple override OnShown() virtual method.
Remember to call the base.Method() at begin of it override.
Hope this little my experience can help!
Leaving designer.cs in peace not only prevents your changes from being overwritten, but also helps other developers by saying that nothing unexpected should come out of it. That being said, there is at least one exception I can think of and that is the one mentioned by author of the post: extension of
Dispose()
method. To my knowledge this code - once generated - will not be overwritten.However, in my opinion much better solution is to override the
Dispose
method and than call thebase.Dispose()
, so that we leave designer.cs clean.You should never modify
.designer.cs
. Period. Your changes will be overwritten without mercy.Update: To be a bit more helpful, C# since v3 (VS 2008) has included partial methods, which many designers will now use to let you implement custom behavior.
this instruction applies to the complete designer.cs file. As all the code written in it is automatically generated.
You should not do any modifications in this file as it can be recreated anytime... this will remove your methods...
If you want to keep the code separate from the form code file, then I suggest to create another file which contains a partial class where you can put all such methods...
Hope it helps...