Connecting Tech Pros Worldwide Forums | Help | Site Map

public Form1() VS Form1_Load

Member
 
Join Date: Mar 2008
Posts: 72
#1: Oct 9 '09
Expand|Select|Wrap|Line Numbers
  1.        public Form1()
  2.         {
  3.             InitializeComponent();
  4.            // some statements
  5.         }


Expand|Select|Wrap|Line Numbers
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.           // some statements
  4.         }
What is the different to put the statement inside public Form1() and Form1_Load ?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,762
#2: Oct 9 '09

re: public Form1() VS Form1_Load


The first happens when the Form1 object is CREATED.

The second happens when the Form1 object is LOADED.
Member
 
Join Date: Mar 2008
Posts: 72
#3: Oct 9 '09

re: public Form1() VS Form1_Load


Quote:

Originally Posted by tlhintoq View Post

The first happens when the Form1 object is CREATED.

The second happens when the Form1 object is LOADED.

if i set my control before executing to some parameter.. should i put it at Form1() or Form1_Load() ?
No different right?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,762
#4: Oct 9 '09

re: public Form1() VS Form1_Load


I don't understand what you are getting at with that question.

maybe this will help
http://msdn.microsoft.com/en-us/libr...form.load.aspx
Member
 
Join Date: Mar 2008
Posts: 72
#5: Oct 9 '09

re: public Form1() VS Form1_Load


Quote:

Originally Posted by tlhintoq View Post

I don't understand what you are getting at with that question.

maybe this will help
http://msdn.microsoft.com/en-us/libr...form.load.aspx

Assume i want the label1.Text =abc

should i put inside
Expand|Select|Wrap|Line Numbers
  1.        public Form1() 
  2.         { 
  3.             InitializeComponent(); 
  4.            label1.Text ="abc";
  5.         } 

Expand|Select|Wrap|Line Numbers
  1.         private void Form1_Load(object sender, EventArgs e) 
  2.         { 
  3.           label1.Text ="abc";
  4.         } 
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,762
#6: Oct 9 '09

re: public Form1() VS Form1_Load


I assume you tried this in your project.
What were your results?
You can't go wrong with trial & error. We learn more through mistakes than through successes.

I tend to put things like that in the Load method, only because it's possible that that some controls (or their properties) don't yet exist if when the form is created but not yet Loaded. Which results in a "null reference exception".
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#7: Oct 9 '09

re: public Form1() VS Form1_Load


I vote for the load event as well.
I try to keep nothing but the InitializeComponent() in the constructor, unless absolutely required.
Newbie
 
Join Date: Sep 2009
Posts: 6
#8: Oct 9 '09

re: public Form1() VS Form1_Load


If you really want to write something like "Label1.Text = "XYZ"", you can write it in form_load or in the constructor. However if you write that in the constructor, just make sure that you write it after InitializeComponent();

Also I normally assign such properties in the designer rather than in the code file.

Your choice :) ... happy coding.
Reply