[英]How to manage delegate of WinForms buttons created at run-time?
讓我們看看下面的代碼,我正在創建一個按鈕列表。
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)
{
// I would like to get the name of the button that was clicked
}
預先感謝您對我們的支持!
發件人對象是一個按鈕,它引發了事件。 所以,只需將其轉換為Button
類型:
void Bt_windowClick(object sender, EventArgs e)
{
Button button = (Button)sender;
// use button
MessageBox.Show(button.Name);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.