views:

242

answers:

4

Several times throughout the course of our current project, the visual studio designer has made modifications that resulted in losing code. For example, event handlers wirings that were set up manually in the designer code were lost. Does anyone know why this happens?

Edit: I understand that editing these files manually is not a good idea, but it has also happened with other event wirings set up through the designer.

+3  A: 

Because it is designer generated and more or less maintained code. It is recommended that you not add or modify code in the designer partial class manually exactly because of the behavior you described (I think it even mentions this in the generated file itself). If you need to wire up event handlers manually then do it in your custom code possibly the constructor of your control.

Quintin Robinson
+1 for the "more or less maintained code". I've seen VS screw up it's own code several times (menu items disappear from the menu, but still exist, images get embedded x times etc.).
Bobby
@Bobby yeah sometimes it's a nightmare, especially with the menu items.. thank god it asks you about undo operations because of the black magic it performs when it undoes designer actions.
Quintin Robinson
+4  A: 

You guys aren't modifying generated code files, are you? Like MyForm.Designer.cs? This is why the baby Jesus gave us partial classes.

Will
+11  A: 

Well for starters read the XML at the top of your designer.cs file.

/// <summary>
/// Required method for Designer support - do not modify
/// contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

Generally you shouldn't be modifying these files as they are auto-generated. It's probably the reason why there is a slight attempt to hide the code within a branch, underneath the main partial class.

I have on occasion found that the process has removed its own auto-generated code that I've had to merge back in. Most commonly it decides it's not going to instantiate custom user controls anymore, so when I start running I get a NullReferenceException.

Really the answer is to put the code somewhere else, like in the constructor before calling the InitializeComponent() method. If fellow developers aren't aware of this, then you should inform them and educate them, the fact that the files are .designer.cs should raise questions even to newer developers as to why the strange extension.

Ian
That comment _used_ to be there (VS2005). In later editions it is absent in most .Designer.cs files. You're suppose to know by now.
Henk Holterman
I know you're not supposed to modify it, but not everyone here does.
Shane Fulmer
@Henk : The comment is still present in VS2008. Not sure about VS2010 though, although I can't see a reason they'd take it out.
Ian
@Shane : Seems like a classic problem of not reading properly though, maybe it just doesn't stand out enough. Honestly I recall a time where I was caught out by the same problem using Java back in my student days.
Ian
@shane: Just make the people who are modifying it be responsible for fixing it when it breaks. Then fire them for incompetence.
Chris Lively
@Chris : Although education works too. You would hope that they'd learn through their mistakes.
Ian
@Ian: fair enough. I'm sure hitting them over the head with a bit of Pavlov might help as well.
Chris Lively
A: 

Write all necessary initialization code only in your own .cs file, you have number of places to do this, like form constructor and form Load event handler.

Alex Farber