Results 1 to 5 of 5

Thread: [RESOLVED] Form Initialization Best Practices

  1. #1

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Resolved [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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width