public Form1() VS Form1_Load

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    public Form1() VS Form1_Load

    Code:
           public Form1()
            {
                InitializeComponent();
               // some statements
            }


    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
              // some statements
            }
    What is the different to put the statement inside public Form1() and Form1_Load ?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    The first happens when the Form1 object is CREATED.

    The second happens when the Form1 object is LOADED.

    Comment

    • MyMarlboro
      New Member
      • Mar 2008
      • 71

      #3
      Originally posted by tlhintoq
      The first happens when the Form1 object is CREATED.

      The second happens when the Form1 object is LOADED.
      if i set my control before executing to some parameter.. should i put it at Form1() or Form1_Load() ?
      No different right?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I don't understand what you are getting at with that question.

        maybe this will help

        Comment

        • MyMarlboro
          New Member
          • Mar 2008
          • 71

          #5
          Originally posted by tlhintoq
          I don't understand what you are getting at with that question.

          maybe this will help
          http://msdn.microsoft.com/en-us/libr...form.load.aspx
          Assume i want the label1.Text =abc

          should i put inside
          Code:
                 public Form1() 
                  { 
                      InitializeComponent(); 
                     label1.Text ="abc";
                  }

          Code:
                  private void Form1_Load(object sender, EventArgs e) 
                  { 
                    label1.Text ="abc";
                  }

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            I assume you tried this in your project.
            What were your results?
            You can't go wrong with trial & error. We learn more through mistakes than through successes.

            I tend to put things like that in the Load method, only because it's possible that that some controls (or their properties) don't yet exist if when the form is created but not yet Loaded. Which results in a "null reference exception".

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I vote for the load event as well.
              I try to keep nothing but the InitializeCompo nent() in the constructor, unless absolutely required.

              Comment

              • akshaymahajan
                New Member
                • Sep 2009
                • 12

                #8
                If you really want to write something like "Label1.Tex t = "XYZ"", you can write it in form_load or in the constructor. However if you write that in the constructor, just make sure that you write it after InitializeCompo nent();

                Also I normally assign such properties in the designer rather than in the code file.

                Your choice :) ... happy coding.

                Comment

                Working...