Is my understanding of code generation in WinForm apps correct?

When you create a program that features a single Windows Form using Visual C#'s Windows Form Designer, three source files are created:

1. program.cs, which:
.....a. does some preliminary configuration of the form and then launches it, and <-- couldn't configuration be done transparently upon launching?
.....b. all the user-written, non-form-related stuff.

2. form1.cs, whose generated code contains that part of the form's class that is to be editted by the user, including "empty" methods:
.....a. the constructor, which calls InitializeComponent, which initializes the form, and
.....b. various user-written, event-driven methods, which:
..........1. handle "operation" of the form, and
..........2. call Application.Exit, which terminates execution of the form.

3. form1.designer.cs, whose generated code contains that part of the form's class that is NOT to be editted by the user:
.....a. the Windows Form Designer generated method InitializeComponent, and
.....b. the method Dispose, which disposes of the form. <-- can't this be editted?

7 Years
Discussion Span
Last Post by sknake

pretty much. Although you can tell it to generate only 1 .cs file with the form generated within it.


1. program.cs contains the main entry point for the application and segregates and provides a code file for all application related code, such as handling unhandled exceptions or initializing certain classes before the "main" form loads. This is in no way related to the forms as you mentioned.
2. yes, except Application.Exit() terminates the application. this.Close() or setting a modalresult on the form terminates the form and not the application, unless it is the main form in which case the app will terminate.
3. Yes, the dispose method could and should be edited to handle cleanup of instantiated IDisposable members that you have added in the userland .cs file. It is also very handy to edit in the case that you have created a descendant of a "TextBox" control named "UltraTextBox" but you don't have it installed on your toolbox. In this case you would drop on a toolbox, go to the designer file, and change the declaration and instantiation of the member to an UltraTextBox. You also may edit the designer file to change the Z-Order of controls which you can probably do through the UI, but its much easier to do in the designer.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.