The difference between InitializeComponent() and Load() of C#

private void InitializeComponent()There private void Form2_Load(object sender, EventArgs e) are the following differences in usage :

  1. private void InitializeComponent(): This is a method used to initialize the form and its controls. It is automatically generated by the designer and is used to set various properties of the form and initialize the controls. This method is usually called in the form's constructor to ensure that the form's initialization is complete before it is displayed.

  2. private void Form2_Load(object sender, EventArgs e): This is an event handling method for handling the event Form2of the form Load. LoadThe event fires when the form loads, indicating that the form is ready for display and interaction. You can perform some operations related to form loading in this event processing method, such as initializing data, updating the interface, and so on.

The difference between the two lies in their role and trigger timing:

  • InitializeComponent()method is called in the form's constructor to initialize form and control properties and set their initial state.
  • Form2_Loadmethod is an event handler that Form2fires when the form load is complete . You can write code in this method to respond to the form loading event and execute the corresponding logic.

In actual use, InitializeComponent()the method is automatically generated by the designer, and you usually don't need to modify it manually unless you need to perform some custom initialization operations. The method Form2_Loadcan be manually written as needed, and specific logic codes can be added in it to respond to the form loading event.

// 主窗体(Form1.cs)
using System;
using System.Windows.Forms;

namespace MultiFormApp
{
    public class Form1 : Form
    {
        private TextBox textBox1;
        private Button openForm2Button;
        Form2 form2;

        public Form1()
        {
            InitializeComponent();
            form2 = new Form2();
        }

        private void InitializeComponent()
        {
            this.textBox1 = new TextBox();
            this.openForm2Button = new Button();
            // 省略其它初始化代码

            // 设置textBox1属性
            this.textBox1.Location = new System.Drawing.Point(10, 10);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(200, 20);
            this.Controls.Add(this.textBox1);

            // 设置openForm2Button属性
            this.openForm2Button.Location = new System.Drawing.Point(10, 40);
            this.openForm2Button.Name = "openForm2Button";
            this.openForm2Button.Size = new System.Drawing.Size(100, 23);
            this.openForm2Button.Text = "Open Form2";
            this.openForm2Button.Click += new EventHandler(this.OpenForm2Button_Click);
            this.Controls.Add(this.openForm2Button);

            // 省略其它控件和布局代码

        }

        private void OpenForm2Button_Click(object sender, EventArgs e)
        {
            
            form2.ShowDialog();
        }

        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    public class Form2 : Form
    {
        public Form2()
        {
         InitializeComponent();
         this.Load += Form2_Load; // 将Form2_Load与加载事件关联
        }

        private void InitializeComponent()
        {
            // 省略初始化代码
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Hello from Form2!");
        }
    }
}

 --------------------------------

(object sender, EventArgs e) It is a standard parameter list of an event processing method, which is used to process the information passed when the event is triggered.

In an event handling method, it is common to use (object sender, EventArgs e)a parameter list, where:

  • senderThe parameter represents the object that triggered the event , which is the sender of the event. Typically used to refer to the object instance that triggered the event.
  • eParameters is an event parameter object that contains information related to the event. The exact event parameter type depends on the type of event being handled.

In C#, the standard parameter list for an event-handling method is defined in terms of the delegate's signature. Most events EventHandlerdefine the signature of their handler methods using a delegate, which accepts (object sender, EventArgs e)a parameter.

By using (object sender, EventArgs e)the parameter list, you can access the event sender and event-related information in the event handling method. This allows you to perform appropriate actions as needed, such as performing different processing based on the identity of the sender, or using the information in the event parameters to perform some specific logical operations.

It should be noted that if the event does not need to pass additional information, EventArgsthe class can be used as an empty parameter. But in practical applications, many events will use custom event parameter classes to carry more specific event information.

In summary, (object sender, EventArgs e)the parameter list in an event handler method allows you to access the sender of the event and event-related information in order to perform appropriate actions as needed.

Guess you like

Origin blog.csdn.net/book_dw5189/article/details/131756048