Avatar of baffledbill
baffledbill
Flag for Canada asked on

VB.NET best practice for where to set initial values on a form

When coding initializations in a Windows form, does it matter whether it's done in the New sub or in the Load event?

For example, when initializing a form I might want to:
- set Me.Text to the application's name and version
- capture the user's name
- create collections of objects for use later in the code
- load settings from a file or database to load into combo boxes or other objects
etc etc

Is it better to do these sorts of things in New or in Load? Or are some things better done in one than the other?
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ChloesDad

The place to do that sort of thing is in a separate subroutine that can then be called from either New or Load as Eric suggests.

Keeping it in a separate subroutine called eg InitialiseDefaults() will also mean that you can call it again within your code if you need to reset the form
baffledbill

ASKER
Thanks Eric and ChloesDad. I do create initialization subroutines when they need to be repeated or called from different moments of the process, such as form reactivation or some other trigger. Even so, I don't think this addresses the question of whether the first initialization would be best called from the form's constructor or its Load event.

Eric, from what you're saying it sounds like the main thing to consider would be the type of object, i.e. whether it's a form object as opposed to a variable, due to availability. Am I understanding your comment correctly?
Éric Moreau

>>Am I understanding your comment correctly?

Yes. Variables and properties can be initialized in the constructor without a problem. Controls are better from the Load event.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
baffledbill

ASKER
That makes sense. Thanks very much for your help.
kaufmed

As long as InitializeComponents is called in the constructor, the controls should be available. The only comment I would make is that there's a small chance that your initialization could break if someone inadvertently detaches the Form_Load handler from the form. You can't disassociate a constructor, so it will always run. If the handler isn't attached, then your handler won't run. Food for thought.
Éric Moreau

I have some 3rd party controls that are not "available" even after the InitializeComponents has been called. Apparently, they postpone initialization until the control is really used (when made visible). You often see this behavior when controls are hosted in a hidden container like a tab control.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kaufmed

Ah.