Windows Develop Bookmark and Share   
 index > Windows Forms General > Where should you begin to write code? constructor or load event?
 

Where should you begin to write code? constructor or load event?

Im just trying to get a handle on where code that does work should be written in a Winforms application.

For example, in my sample Winforms application below, I have to binddata from a table in a database to a tree, and I do all that dataBinding etc in a BindTree() method. So do i call the BindTree() method after the InitializeComponent() in the forms constructor, or do i call the method in the Load event of the form?

Is there a best practice solution? Is there an advantage of one vs. the other?

Thanks

namespace Sample
{
    public partial class Module : Form
    {

        public Module()
        {
            InitializeComponent();
             // does the BindTree() go here?
        }

        private void Module_Load(object sender, EventArgs e)
        {
            //or do i create the load event and put the method here?
            BindTree();
        }

You are so wise...like a miniature budha covered in fur. -Anchorman
YoungEngineer  Friday, May 15, 2009 5:23 AM
You should always favor the constructor. Code in the Load event is generally less efficient. By the time it runs, the window has been created. And updating properties now requires updating the internal control data as well as the window. ListView.Items.Add() for example is *much* slower.

Exceptions are a problem too in the Load event, there is no good way to let the caller know that initializing the form failed.

There is one case where you should use Load. Only then do you know the real Location and Size of the form. Which might be different from the programmed value because of StartPosition and automatic rescaling. Any code that lays out your form (say, positioning controls) should go in Load.

Hans Passant.
nobugz  Friday, May 15, 2009 11:00 AM
Both will work.

Check this link it will help

http://www.informit.com/articles/article.aspx?p=101720&seqNum=3
Thanks,
A.m.a.L
.Net Goodies
Remember to click "mark as answered" when you get a correct reply to your question
A.m.a.L - aditi.com - Think Product  Friday, May 15, 2009 5:34 AM
You should always favor the constructor. Code in the Load event is generally less efficient. By the time it runs, the window has been created. And updating properties now requires updating the internal control data as well as the window. ListView.Items.Add() for example is *much* slower.

Exceptions are a problem too in the Load event, there is no good way to let the caller know that initializing the form failed.

There is one case where you should use Load. Only then do you know the real Location and Size of the form. Which might be different from the programmed value because of StartPosition and automatic rescaling. Any code that lays out your form (say, positioning controls) should go in Load.

Hans Passant.
nobugz  Friday, May 15, 2009 11:00 AM
I did see a big difference in populating my treeview in the onload vs. the constructor, but i just wasnt sure what the 'correct' method was. directly writing code in the constructor didnt look 'right' i guess.

i will call my methods from the constructor and remove the load event.

thanks noBugz.
You are so wise...like a miniature budha covered in fur. -Anchorman
YoungEngineer  Friday, May 15, 2009 7:59 PM

You can use google to search for other answers

Custom Search

More Threads

• Scroll Bars on Windows Form
• RTF Image Object
• Follow-up to Colbert's post re controls in the DataGridView column headers
• Forms to fit monitors with different screen resolution
• Multipage report only shows/print first page
• CheckedListBox.ItemCheck
• How to disable editing for a drop down list item in property grid?
• How to access data from .csv file from a network using Visual basic 2008
• GridView Question - Filter?
• TabControl and Menu color settings question