Q. I wonder if there are any times I shouldn't directly edit in Forms Designer-generated code or other times when you can do nifty and useful things through the .cs file that aren't possible with only the Forms Designer. Also, are there things I SHOULD be able to edit or add but Visual Studio doesn't allow or overwrites on subsequent builds or gives unpredictable results.

Asked by Austin Avrashow. Answered by the Wonk on January 10, 2003


Curious Customer: How much is this Porsche Boxster? Iím wondering if I can afford it.

Snooty Porsche Salesman: If you have to ask, you canít afford it.


The InitializeComponent function contains the serialized form of the designerís state for a component. While itís true that the InitializeComponent function looks like your language of choice (either C# or VB.NET), thatís merely an implementation detail. Just as you wouldnít edit a .doc without Word or a .ppt without PowerPoint, the InitializeComponent method is not yours to edit. If you do and you step outside the undocumented lines of what the designer expects to see in that method, you could well lose the entire contents of the method (as Iíve done a number of times). If you want to augment what InitializeComponent does, you should do it in the constructor, as the generated code points out:


public Form1() {

  // Required for Windows Form Designer support



  // TODO: Add any constructor code after InitializeComponent call



#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent() {





I know the temptation is, because it looks like code, to edit it directly, but you do so at your own risk. If you must edit it directly, I recommend source code control and/or backups before trying it until you get a feel for what the designer is OK with.


I have feedback on this Ask The Wonk answer