Почему инициализатор не работает для воссоздания событий, если они были удаленыC#

Место общения программистов C#
Ответить
Anonymous
 Почему инициализатор не работает для воссоздания событий, если они были удалены

Сообщение Anonymous »

Помогите мне понять следующую проблему. У меня есть событие numericupdown_valuechanged, которое работает. Если я нажимаю кнопку1, я удаляю это событие из числового. Так что это событие больше не работает. Когда я нажимаю кнопку2, я вызываю метод инициализации.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();
}
}

my .designer file
partial class Form1 {
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

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

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
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;
}


Подробнее здесь: https://stackoverflow.com/questions/475 ... re-removed
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»