|
-
Apr 23rd, 2013, 01:02 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Form Initialization Best Practices
Hi All,
I was wondering where the best place to do form initialization is. I've always performed this in the Form's load event. However, It seems like there are times (especially for run time added controls) that maybe some code might belong in the New method?
Any thoughts?
Thanks,
Nick
-
Apr 23rd, 2013, 02:19 PM
#2
Re: Form Initialization Best Practices
FormLoad caters for just about everything except the rare occasions on which the form must be visible, in which case FormShown does the job. New is usually reserved for non-Form classes.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 23rd, 2013, 08:41 PM
#3
Re: Form Initialization Best Practices
The constructor is executed when you create the object using the New keyword. The Load event handler is executed when you call Show or ShowDialog, just before the form becomes visible. The Shown event handler is executed when you call Show or ShowDialog, just after the form becomes visible.
The first consideration is the fact that constructors should be executed quickly. That means that you should NOT be doing things like retrieving data from databases and files in a constructor. With regards to forms and controls, it's worth remembering that all the designer-generated code that creates and initialises those controls is executed in the constructor. That would suggest that adding dynamic controls should be done in the constructor rather than the Load event handler. That said, if you're creating controls in code rather than in the designer then that is probably because they are influenced by some external factor, which suggests it should be done in the Load event handler rather than the constructor.
-
Apr 23rd, 2013, 09:45 PM
#4
Thread Starter
Hyperactive Member
Re: Form Initialization Best Practices
Thanks All. I like the delineation of adding in any item that is not influenced by external data (i.e. populating a drop down that might have the numbers 1-20 in it) to the constructor. While also placing any items that require non-static info to be added or updated in the load method. And finally adding anything that should run after the form is displayed in the form Shown event.
I think everything is covered, but I welcome any more input.
-
Apr 23rd, 2013, 10:06 PM
#5
Re: [RESOLVED] Form Initialization Best Practices
It's also worth noting that you should generally do all the control creation and configuration that you can in the designer. Some things are not possible, e.g. you mention adding numbers to a drop-down. If you add items in the designer then they will be Strings, so adding actual numbers must be done in code. Anything that can be done in the designer though, should be.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|