About form load

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!

I have a demo program that have this event handler Form1_Load below.
I just wonder is it any difference if the call to this Form1_Load is removed
and the call to InitGrid() is done
in the constructor instead.
So what is the point having this method Form1_Load if the same can be done
in the constructor?
private void Form1_Load(object sender, System.EventArgs e)
{
InitGrid();
}

//Tony
 
tony said:
Hello!

I have a demo program that have this event handler Form1_Load below.
I just wonder is it any difference if the call to this Form1_Load is removed
and the call to InitGrid() is done
in the constructor instead.
So what is the point having this method Form1_Load if the same can be done
in the constructor?
private void Form1_Load(object sender, System.EventArgs e)
{
InitGrid();
}

//Tony
I believe it will be just fine for you to put the call to InitGrid in
the constructor, as long as you put the call below the
InitializeComponent() method. This ensures that all the components on
the form are instantiated before any attempt at accessing them from your
method. As far as why there is Form_load event, the reason is you can
construct the form ahead of time but not have the form "load" per say
and eventually you can call Show() or ShowDialog() and then the event
will be called. This separation gives you the ability to separate
initialization code in the constructor from code that will update the UI
before it is shown in the form_load event.

Hope this helps.

Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top