Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

winforms - Changing autogenerated code in a C# Windows Forms Application project

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...