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
623 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 41
C# DataTable (in memory) vs SQL Lookups 4 67
Need to find a web event on the IIS server 4 48
Import a excel sheet in a grid 2 42
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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