如果 InitializeComponent 被移除,为什么它们不适用于重新创建事件

Why InitializeComponent not works for recreate events if they were removed

请帮助我理解以下问题。

我有一个演示 C# wiforms .net 项目。我有一个有效的 numericUpDown_ValueChanged 事件。如果我单击按钮 1,我会从数字中删除该事件。所以这个事件不再起作用了。当我单击 button2 时,我调用了 InitializeComponent 方法。

问题:为什么这不会为我的 numericUpDown 控件重新创建事件? 是否有机会将事件分配回控件?

我不想这样做:

numericUpDown1.ValueChanged += numericUpDown1_ValueChanged;

我的代码

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
        numericUpDown1.ValueChanged -= numericUpDown1_ValueChanged;
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
        Console.WriteLine(numericUpDown1.Value);
    }

    private void button2_Click(object sender, EventArgs e) {
        InitializeComponent();
    }
}

我的 .Designer 文件

partial class Form1 {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        this.button1 = new System.Windows.Forms.Button();
        this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
        this.button2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(80, 139);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // numericUpDown1
        // 
        this.numericUpDown1.Location = new System.Drawing.Point(123, 47);
        this.numericUpDown1.Name = "numericUpDown1";
        this.numericUpDown1.Size = new System.Drawing.Size(120, 20);
        this.numericUpDown1.TabIndex = 1;
        this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(155, 93);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 2;
        this.button2.Text = "button2";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.numericUpDown1);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.NumericUpDown numericUpDown1;
    private System.Windows.Forms.Button button2;
}

第二次调用InitializeComponent不正确。虽然它有效,但如果处理不当,它会在您的程序中产生很多错误。

当您第二次调用 InitializeComponent 时,您添加了另一个 numericUpdown 控件实例以及表单中每个 UI 元素的第二个实例。
这些第二个实例被放置在与第一个实例相同的位置,但它们在第一个实例之下(在 Z 顺序上)。
控制变量(numericUpDown1、button1、button2)现在引用这些新实例而不是原始实例。

numericUpDown 的第二个实例获得分配的事件处理程序。
但是您不能单击它,因为它位于第一个事件处理程序仍处于禁用状态的事件之下。

如果您想第二次调用 InitializeComponent,您需要对其进行大量编辑,或者首先从表单的控件集合中删除所有内容,毕竟这很可能会搞砸所有内容。

对于第一个选项,您可能会遇到性能问题(控件的创建不是免费的),对于第二个选项,您可以自己查看设计者的代码注释,提醒您如果尝试这样做可能会发生多糟糕的事情编辑或弄乱此功能。

为了证明这一点,只需将您的 button2 点击处理程序更改为

private void button2_Click(object sender, EventArgs e)
{
    InitializeComponent();

    // Show the problem with a second call to InitializeComponent
    // The UD control moved is the second one with its handler active.
    numericUpDown1.Location = new Point(0,0);
}

现在您可以看到两个 numericUpDown 控件,如果您单击零位置的那个,您会看到它对您的点击做出反应,而第一个仍然没有反应。

在事件处理程序被删除后重新访问它的适当方法是通过 += 运算符。您可以阅读此答案以发现 if the event handler has already been added