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
575 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
Comment Utility
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
Comment Utility
Thank you for all the replies!
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now