deleyd
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):
We start with
So that instantiates an instance of System.Windows.Forms.Form named Form1
The code
The CONSTRUCTOR for the newly instantiated class Form1 (which is an instance of the class System.Windows.Forms.Form) gets run which calls
The InitializeComponent() code for Form1 is generated by Visual Studio in the Form1.Deisgner.cs file:
So I'm just left with questions Q2 and Q3 above.
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...
}
}
}
We start with
Application.Run(new Form1());
which creates an instance of class Form1 called I don't know, it's not assigned a name, it's just swallowed by Application.RunSo that instantiates an instance of System.Windows.Forms.Form named Form1
The code
public partial class Form1 : System.Windows.Forms.Form
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();
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);
}
And that's where Form1_Load comes from.So I'm just left with questions Q2 and Q3 above.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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
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).
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)