1

i have a constructor:

public Form1(string startDate, string endDate, string cbQCValues,string cbAnalytes, string cbInstruments,bool copy_of_form)
    {
        InitializeComponent();
        dateStart.Value = Convert.ToDateTime(startDate);
        dateEnd.Value = Convert.ToDateTime(endDate);


        GenerateGraph();
    }

and i also have an onLoad

  private void Form1_Load(object sender, EventArgs e)
            {

               string qcvalues_query = "SELECT DISTINCT name FROM qvalues ORDER by name";
               string analytes_query = "SELECT DISTINCT compound FROM qvalues ORDER by compound";
               string instruments_query = "SELECT DISTINCT instrument FROM batchinfo WHERE instrument <> '' AND instrument is not Null ORDER by instrument";
...
...
}

what gets executed first? i need to make sure that the onLoad gets executed first

is this posible?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Possible duplicate of [What setup code should go in Form Constructors versus Form Load event?](http://stackoverflow.com/questions/2521322/what-setup-code-should-go-in-form-constructors-versus-form-load-event) – Jim Fell May 11 '16 at 13:53

5 Answers5

5

Of course the constructor gets executed first, pretty basic rule in any OOP language. Whether it will finish first is an open question. Technically it is possible for the constructor to tinker with a Form class property that requires the Handle to be created. Like using the Handle property, to keep it simple. That will trigger OnLoad and the Load event which will then run to completion before the constructor is completed.

It's rare but possible.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
4

The constructor - Form1_load is an instance method which requires a valid instance in order to be called.

JakeSays
  • 329
  • 1
  • 6
  • Yes, constructors always get called first. You can't call the load function unless you have an instance of the form. – Justin Nov 08 '10 at 22:52
1

it is not possible for onload to be executed first, you need to put that code in the constructor execution path

Alex Lo
  • 1,299
  • 8
  • 10
0

The object needs to be constructed before it can handle any events (e.g. Load). Form1_Load can not execute before Form1 has been constructed. To answer your question, it is not possible for Load to occur before the form has been constructed. If all you are doing in the Load function is initializing those strings you can pull them out of Load and put them either in the constructor or where they are declared. e.g.

public partial class Form1
{
    private string qcvalues_query = "SELECT DISTINCT name FROM qvalues ORDER by name";
    private string analytes_query = "SELECT DISTINCT compound FROM qvalues ORDER by compound";
    private string instruments_query = "SELECT DISTINCT instrument FROM batchinfo WHERE instrument <> '' AND instrument is not Null ORDER by instrument";

    <rest of code here>
}
Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41
0

This has been pretty well answered, but one thing to notice that might help is that the names follow a pretty common convention. Typically, event handler methods are named for the event publishing object and the event...Form1_Load looks like it's no different.

That method is invoked by the Load event of the instance (Which can only exist after the constructor call is made).

Steven
  • 1,260
  • 9
  • 22