By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,022 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,022 IT Pros & Developers. It's quick & easy.

Is my understanding of code generation in WinForm apps correct?

P: 93
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?
Feb 26 '09 #1
Share this Question
Share on Google+
1 Reply


tlhintoq
Expert 2.5K+
P: 3,525
Not a bad start at all. A couple things to keep in mind...

While everyone will tell you that global variables are the evil devil spawn that will destroy the known universe... They are sometimes incredibly practical. Any public property defined in Program.cs will be available to all of the children of Program which would be all the forms and custom controls. I use a PrivlegesObject class in the Program.cs to hold all the user priveleges (can they edit the configuration, permission to exit the application and so on). This way any form, any class can check the Program.Priveleges.ExitApp property to see if the user is allowed to do so.

Program.cs contains the one and only Main() method that really is the practical start of your application. The Main() method then launches Form1 or MyStartupWizardForm or whatever form you want to have be the startup form. By default it is Form1, but it doesn't have to be. You can have a working program that launches Form1, but you can work on an upgrade that will launch Form101 when you have the upgrade done. Just change which form is launched in the Program.cs

Program.cs in my case also contains the code to make sure my applications are single instance only. This is also where I have a splash screen launch from. This way the user gets a splash screen right away no matter what. If your Form1 is going to take 10 seconds to read information and set up the user isn't left wondering if the application launched or not, hung or not...

I think of Form1.cs as the logic of Form1 and Form1.Designer.cs as the UI of the Form. If its for looks then it tends to go into the Designer. If it is for behavior then it goes in Form1.cs.
* - Control creation goes in the designer.
* - Event response goes into the Form1.cs -
But this is a really loose way of looking at it. Form1.cs can very easily create a new control and place it on the form programmatically.

Yes you can edit the Dispose() method. But making changes INSIDE the region of automatically created code can be funky. This is why the Dispose() method is outside of the generated code region.
Expand|Select|Wrap|Line Numbers
  1.         protected override void Dispose(bool disposing)
  2.         {
  3.             if (disposing && (components != null))
  4.             {
  5.                 components.Dispose();
  6.             }
  7.             base.Dispose(disposing);
  8.         }
  9.  
  10. #region Windows Form Designer generated code
  11. // .... generated code for the controls
  12. #endregion
Feb 26 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.