Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Form1_Load() invoked by the Load event in Windows Forms?

I'm a rookie in Windows Forms Applications. I have created a Form without any controls added. The following code is generated by the VS compiler

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(328, 301);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
}

I have found that Load is an event and the Form1 class has Form1_Load() function with arguments. But I couldn't find how that function is invoked by the Load event. How does this call occur?

like image 505
babybob Avatar asked Dec 02 '25 10:12

babybob


1 Answers

An event has a certain signature, and the event handler must match this signature.

The default EventHandler delegate has a signature of object sender, EventArgs e, so the event handler method must have this exact same signature:

private void Form1_Load(object sender, EventArgs e)

If it wouldn't, but for example simply be a parameterless method:

private void Form1_Load()

Then this code wouldn't compile:

this.Load += new System.EventHandler(this.Form1_Load);

No overload for 'Form1_Load' matches delegate 'System.EventHandler'

As for who is raising this event and how the arguments are assigned, that's the WinForms engine doing that for you.

like image 114
CodeCaster Avatar answered Dec 05 '25 00:12

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!