请帮助我理解以下问题。
我有一个演示C#wiforms .net项目。我有一个numericUpDown_ValueChanged事件可以工作。如果我单击button1,我会从数字中删除该事件。所以这个事件不再起作用了。当我单击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;
}
答案 0 :(得分:-1)
第二次调用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