我正在使用C#窗體並需要一些幫助。我有一個按鈕可以創建其他按鈕並將它們添加到列表的「按鈕」中。我需要讓每個按鈕在點擊時自行銷燬。刪除動態生成的按鈕
//create new button
Button newButton = new Button();
newButton.Name = "aButt"+buttNum;
Debug.WriteLine(newButton.Name);
buttNum++;
newButton.Text = "Button!";
newButton.Height = 50;
newButton.Width = 50;
//controls where the new button gets placed
if (curX > 9)
{
curX = 0;
curY++;
//defines the point the button spawns
newButton.Location = new System.Drawing.Point((curX * 55)+10, curY * 55);
//increments X to avoid placing a button on top of another
curX++;
}
else
{
newButton.Location = new System.Drawing.Point((curX * 55) + 10, curY * 55);
curX++;
}
newButton.UseVisualStyleBackColor = true;
newButton.Click += new System.EventHandler(this.removeThisButton);
buttons.Add(newButton);
this.Controls.Add(newButton);
我有事件監聽器的設置,但由於發件人沒有關於按鈕本身的實際信息,我不知道如何擺脫它。
任何幫助表示讚賞!
這是一個Windows Forms應用程序嗎? WPF?我想添加更多特定標籤來吸引專家。 – neontapir