Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
691 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 1000 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 1000 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 45

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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