Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

What's the relation between Form.Load, Form.OnLoad, Form1_Load? Also OnShown?

Q1. How does a call to the Form.Load event end up being a call to the Form1_Load method? (OK I answer this myself below, so my next question is:)

Q2. What's the relation between the Form.Load event and the Form.OnLoad method?

Q3. How is the form's OnShown method called (who calls it)?


Let's see if I can trace this (I removed all 'using' statements to help clarify this for me):
namespace TestForm
{
  static class Program
  {
    [System.STAThread]
    static void Main()
    {
      System.Windows.Forms.Application.EnableVisualStyles();
      System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
      System.Windows.Forms.Application.Run(new Form1());
    }
  }
  public partial class Form1 : System.Windows.Forms.Form
  {
    public Form1()  //CONSTRUCTOR
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
      //code...
    }
     protected override void OnShown(EventArgs e)
     {
       //code...
     }
  }
}

Open in new window


We start with
Application.Run(new Form1());

Open in new window

which creates an instance of class Form1 called I don't know, it's not assigned a name, it's just swallowed by Application.Run

So that instantiates an instance of System.Windows.Forms.Form named Form1

The code
public partial class Form1 : System.Windows.Forms.Form

Open in new window

somehow defines code at compile time for a class that doesn't exist until run time.

The CONSTRUCTOR for the newly instantiated class Form1 (which is an instance of the class System.Windows.Forms.Form) gets run which calls
InitializeComponent();

Open in new window


The InitializeComponent() code for Form1 is generated by Visual Studio in the Form1.Deisgner.cs file:
    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // Form1
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(284, 264);
      this.Name = "Form1";
      this.Text = "Form1";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);
    }

Open in new window

And that's where Form1_Load comes from.

So I'm just left with questions Q2 and Q3 above.
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hello:
A Q2: Form.OnLoad call the Load event if it have a event handler instance.
A @3: When the form is Show (Im not sure if it is called more that one time)
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Note that obj_Shown is never fired, because we overrode the OnShown method and didn't call the base's implementation.

Correction ... obj1_Shown is never fired because MyObject overrides MyBaseObject's OnShown method.
After all this explaining it might also help you to read page.

see for the Page Life Cycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx

@poor_beggar

That link's for an ASP.Net page cycle, it doesn't really apply as the question pertains more to Windows Forms (and some of the more fundamental concepts of Object Oriented programming).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial