473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

public Form1() VS Form1_Load

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 ?
Oct 9 '09 #1
7 8065
tlhintoq
3,525 Expert 2GB
The first happens when the Form1 object is CREATED.

The second happens when the Form1 object is LOADED.
Oct 9 '09 #2
@tlhintoq
if i set my control before executing to some parameter.. should i put it at Form1() or Form1_Load() ?
No different right?
Oct 9 '09 #3
tlhintoq
3,525 Expert 2GB
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
Oct 9 '09 #4
@tlhintoq
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.         } 
Oct 9 '09 #5
tlhintoq
3,525 Expert 2GB
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".
Oct 9 '09 #6
Plater
7,872 Expert 4TB
I vote for the load event as well.
I try to keep nothing but the InitializeComponent() in the constructor, unless absolutely required.
Oct 9 '09 #7
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.
Oct 9 '09 #8

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

Similar topics

7
by: Harry J. Smith | last post by:
In my program VPCalc, Public procedures in a Form are not visible to other Modules. Is this how it should be? -Harry http://home.netcom.com/~hjsmith
5
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say...
1
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small maybe you will laugh when you get it that simply I have 2 forms let’s say Form1 (main form) and Form2 and...
3
by: | last post by:
All the fields in webforms are automatically declared as protected. If I change them to public, they are converted back to protected. Why is this happening ? Thanks
6
by: ElenaR | last post by:
I have three variables which I have made Public. The other forms in my project are still not able to read these variables. Am I missing something?
4
by: ST | last post by:
From Form1 I call a public sub in a module. The sub writes on Form1. Problem: the call creates a new Form1 and writes on it, instead of writing on the one already shown Form1. I have tried to...
28
by: Maarten | last post by:
hi all, i have a settingform(form4) this form opens from an mdi parent form1 what i want do do is, when i make change in form4 (adjust settings), then a listview in the mdi parent form1...
2
by: Hexman | last post by:
Well, back for another question. I'm doing some calculations in a module (the reason in a module is because it is an I-O module - reading csv files, Access DB, etc.). (If not in a Module, where...
2
by: Carla Simeoni | last post by:
In general I have two procedures which are called at the beginning of a Form: Form1() { InitializeComponent(); ..... } and Form1_Load(); What is the difference ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.