C# 如何管理在运行时创建的WinForms按钮的委托?

C# 如何管理在运行时创建的WinForms按钮的委托?,c#,winforms,events,C#,Winforms,Events,让我们看看下面的代码,我在其中创建了一个按钮列表 List<System.Windows.Forms.Button> buttons = new List<System.Windows.Forms.Button>(); for(int i = 0; i < 5; i++) { buttons.Add( System.Windows.Forms.Button>() ); buttons[i].Name = "button_" + i.To

让我们看看下面的代码,我在其中创建了一个按钮列表

List<System.Windows.Forms.Button> buttons = new List<System.Windows.Forms.Button>(); 

for(int i = 0; i < 5; i++) {
    buttons.Add(  System.Windows.Forms.Button>()  );
    buttons[i].Name = "button_" + i.ToString();
    this.Controls.Add(buttons[i]);
    buttons[i].Click += new System.EventHandler(this.Bt_windowClick);
}

提前感谢您的支持

发送方对象是一个按钮,它引发了事件。因此,只需将其转换为
按钮
类型:

void Bt_windowClick(object sender, EventArgs e)
{
    Button button = (Button)sender;
    // use button
    MessageBox.Show(button.Name);
}

Sender对象是引发事件的按钮。因此,只需将其转换为
按钮
类型:

void Bt_windowClick(object sender, EventArgs e)
{
    Button button = (Button)sender;
    // use button
    MessageBox.Show(button.Name);
}