views:

93

answers:

3

When you open a new C# Windows Forms Application project in Visual Studio 2008, you get a lot of autogenerated code (AssemblyInfo.cs, Resources.Designer.cs, Settings.Designer.cs, Form1.Designer.cs, Form1.resx, Program.cs).

Beside adding components from the Toolbox to Form1.cs[Design] and code to Form1.cs, what files can you change? And how? And what files should be left as they are?

PS: This is a vast subject, as far as I can tell. I don't expect an exhaustive answer. Any information you care to share will do. I also want to mention that I'm a C# beginner and only want the most basic information.

PPS: In case you're thinking about answering "Search the internet" or something like that, don't. I have searched the internet and will do so again and again until I find the answers I'm looking for. I have found some useful information, but I feel I need more. I think it's likely I will get that at stackoverflow.com.


Update 1: This question has been answered. I've marked the answer from sh_kamalh as accepted. I would like to mark the answers from David and Hans Passant as accepted as well, but I don't think you can mark more than one answer as the accepted one. Or can you?

From what I can dedcue from the answers, you should never change the autogenerated code in a C# Windows Forms Application project in Visual Studio 2008. I don't understand everything in the answers, though, and may have overlooked something.

If you don't agree with the summary right above or the answers below, feel free to add a contradictory answer. Or if you have an answer that supplement the answers below, feel free to add that answer. I'm sure there's more to be said on this subject.


Update 2: It seems you can also write your own version of a C# Windows Forms Application project (see Davids answer below, which I forgot about when I wrote "Update 1"). It also seems you should start from scratch if you do.

+3  A: 

Most of those files are templated and you can modify their inclusion in a new project and what is contained in the files. You can even create your own templates for new project types or items. http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

As far as modifying what is already there, unless you're sure that every project you're going to work on is going to need those changes I wouldn't recommend doing that, it's easier to just create a new template for a different project/file type.

David
Thanks! I've added the link to my Favorites folder. The article will most likely be useful later when I know more about C#. Right now, though, I find it overwhelming, because there are too many words I don't understand well enough yet. Before I can tackle this kind of longer articles, I need to read several shorter articles, covering more basic C# concepts. --- PS: I've marked the answer from sh_kamalh as the accepted one. I would like to mark this answer as well, but I don't think you can mark more than one answer as the accepted one. Or can you?
matsolof
+1  A: 

Why do you want to change the auto-generated files?
In case of forms you should not change .Designer.cs or the .resx files because they will be generated everytime you change the form so whatever changes you do will be overwritten next time the code is regenerated.
If you want to change something in Designer.cs, you can do that change in the original cs file because the class is marked as partial which means that the code from the files Form1.cs and Form1.Designer.cs is combined to generate the final code.
If you want to add your resources add a resource file and leave the generated .resx file intact. For AssemblyInfo.cs you can change the general information of the assembly.

Long story short just leave the generated files intact.

sh_kamalh
Thank's for the answer! I don't know if I want to change the autogenerated files or not, becuase I don't understand the code in them well enough yet. For now, I simply want to know which autogenerated files you can change and which autogenerated files you can't change. As far as I understand the answers I've got so far, the best approach is to never change the autogenerated files.
matsolof
+1  A: 

The auto-generated files:

  • AssemblyInfo.cs : the attributes inside it are set by Project + Properties, Application Tab, Assembly Information button. Editing the file directly is okay, the IDE doesn't easily get confused by it.

  • Settings.Designer.cs : auto-generated from the Settings.settings file which in turn is generated from the Settings designer. Any edits to this file will be lost quickly.

  • Resources.Designer.cs : auto-generated from the Resources.resx file which in turn is generated from the Resources designer. Any edits to this file will be lost quickly.

  • Form.Designer.cs : auto-generated by the form designer. Editing this file is possible, there is no independent file that stores the form layout. The design view is generated by running this code at design time and using Reflection to recover the form and control properties. And saved again when you close the designer, it re-generates the InitializeComponent method and the control variables. Getting your code changes to survive this process requires writing the code exactly the way the designer itself would. Which is fairly untrivial when you make changes beyond simple property value changes. There's also considerable risk, an unwise change can easily cause an exception. When that happens at design time, you get the infamous WSOD (White Screen Of Darn) which displays an often very cryptic exception and tells you that you cannot design your form anymore.

You can learn more about the way the designer generates code by observing the changes it makes to InitializeComponent when you modify the form in the designer. Getting this right is certainly not beginner material, you are probably have more pressing things to learn while you get up to speed on .NET programming. The designers are there to make your life easier and to minimize the risk of getting it wrong.

Hans Passant
Thank's for the answer! I don't understand all of it, but the parts I do understand are illuminating. --- PS: I've marked the answer from sh_kamalh as the accepted one. I would like to mark this answer as well, but I don't think you can mark more than one answer as the accepted one. Or can you?
matsolof