Solved

What's the difference in c# for putting start up code in and near InitializeComponent() versus Form_Load event?

Posted on 2014-04-24
6
596 Views
Last Modified: 2014-04-24
I'm new to C# and have many events to run that will affect the form's properties and text boxes.  Some text boxes are loaded with a string to a file path and other commands I have adjust the height and width of the form.

Am I correct that all of these events and changes should occur only in Form_Load event and not necessarily near public frmMain() { InitializeComponent():

Is it a bad habit to put code there and if so, then what necessarily should go in after InitializeComponent()?

Thanks for any replies.
0
Comment
Question by:stephenlecomptejr
6 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40020973
It depends on what the code does.

In some situations, such as when you want to be able to pass information to the Form when you instantiate it (call New on the form), you might have many constructors. That is what public frmMain() is called, because this is the method through which you "construct" the form.

public frmMain()
public frmMain(int userID)
public frmMain(string username)

But there is only one Form_Load event.

Is you do not have many constructror, as is most often the case with forms, then you can put everything in the Load event.

If you have many constructors however, this is the place to make the difference between calling the form with parameters or not. What is common to all the ways of instantiating the form then goes in the event. If you do use both, then be aware that the code in the constructor is called before the code in the Load event.

As for putting the code before or after InitializeComponent, you could in theory put code before. But the controls do not exist yet on the form at that time, because this is what InitializeComponent does. Since there are very few reasons to do stuff in the form before its controls are created, there are also very few reasons to put code before InitializeComponent.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 40020976
You may place code in the Form's constructor such as you have indicated in your question. All code that you do place in the constructor should appear after the InitializeComponent method call. This is because InitializeComponent() initializing all the forms controls and this should happen before you attempt to change any control values.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 125 total points
ID: 40021088
One major difference.
Near the InitializeComponents then the code you add will always run (because it is in the constructor).
The FormLoad will only be called if you actually display the form.

eg.
{
   MyForm f = new MyForm();  // this runs code near InitializeComponents
  //The form load event never runs
}

and
{
   MyForm f = new MyForm();  // this runs code near InitializeComponents
   f.Show();  //The form load event now will be called
}
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40021129
A small comment on Andy's answer.

The code near the InitializeComponents will always run if there is only one constructor. But if you have many constructors, it depends on the constructor that is called and the way the constructors interact one with each other.

And there is very little reason to call the constructor on a Form if one does not display the form, so I would change one sentence: The FormLoad will only be called WHEN you actually display the form.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40021232
Thank you for all the replies!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40021882
>>And there is very little reason to call the constructor on a Form if one does not display the form

Agreed (I can only think of rather artificial ways that would happen) as to why but there is a significant difference as to when and if that block of code would be run.

ps. how often does one see mediocre code, or bad code or really horrendous code ?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question