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
605 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 63

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
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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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