C# 如何在运行时删除按钮?
我的C winform项目中有一个问题。 在我的项目中,我有一个函数,可以在运行时创建一个新按钮。因为有时候我制作了太多的按钮,所以我想编写一个函数来删除我想在运行时删除的按钮。 也许有人已经有了这个功能C# 如何在运行时删除按钮?,c#,winforms,button,C#,Winforms,Button,我的C winform项目中有一个问题。 在我的项目中,我有一个函数,可以在运行时创建一个新按钮。因为有时候我制作了太多的按钮,所以我想编写一个函数来删除我想在运行时删除的按钮。 也许有人已经有了这个功能 private void button2_Click(object sender, EventArgs e) { Button myText = new Button(); myText.Tag = counter; myText.Locatio
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
}
这就是我在运行时创建按钮的方式。您应该能够使用this.Controls.RemovemyText 您应该能够使用this.Controls.RemovemyText
public Button myText ; // keep public button to assign your new Button
private void buttonAdd_Click(object sender, EventArgs e)
{
myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
}
private void buttonRemove_Click(object sender, EventArgs e)
{
if(Button != null && this.Controls.Contains(myText))
{
this.Controls.Remove(myText);
myText.Dispose();
)
}
如果您想在按delete键时删除,您可以使用按键向下事件,如下所示
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if(Button != null && this.Controls.Contains(myText))
{
this.Controls.Remove(myText);
myText.Dispose();
)
}
}
如果您想在按delete键时删除,您可以使用按键向下事件,如下所示
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if(Button != null && this.Controls.Contains(myText))
{
this.Controls.Remove(myText);
myText.Dispose();
)
}
}
要删除您添加的最后一个按钮,可以使用以下方法:
//a list where you save all the buttons created
List<Button> buttonsAdded = new List<Button>();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
//add reference of the button to the list
buttonsAdded.Insert(0, myText);
}
//atach this to a button removing the other buttons
private void removingButton_Click(object sender, EventArgs e)
{
if (buttonsAdded.Count > 0)
{
Button buttonToRemove = buttonsAdded[0];
buttonsAdded.Remove(buttonToRemove);
this.Controls.Remove(buttonToRemove);
}
}
要删除您添加的最后一个按钮,可以使用以下方法:
//a list where you save all the buttons created
List<Button> buttonsAdded = new List<Button>();
private void button2_Click(object sender, EventArgs e)
{
Button myText = new Button();
myText.Tag = counter;
myText.Location = new Point(x2,y2);
myText.Text = Convert.ToString(textBox3.Text);
this.Controls.Add(myText);
//add reference of the button to the list
buttonsAdded.Insert(0, myText);
}
//atach this to a button removing the other buttons
private void removingButton_Click(object sender, EventArgs e)
{
if (buttonsAdded.Count > 0)
{
Button buttonToRemove = buttonsAdded[0];
buttonsAdded.Remove(buttonToRemove);
this.Controls.Remove(buttonToRemove);
}
}
你想用另一个按钮删除它吗?你是如何实现它的?不,我想标记一个新的按钮,然后按键盘上的prees on delete,按钮将被删除…请参阅UnhandledException的答案。将按钮引用放入类级别变量中,或者由于您正在创建许多按钮,因此将按钮字典作为类级别变量,然后您可以从字典中按名称获取动态创建的按钮引用,然后使用。删除方法您是否尝试通过另一个按钮的操作删除它?你是如何实现它的?不,我想标记一个新的按钮,然后按键盘上的prees on delete,按钮将被删除…请参阅UnhandledException的答案。将按钮引用放入类级别变量中,或者由于您正在创建许多按钮,因此将按钮字典作为类级别变量,然后您可以从字典中按名称获取动态创建的按钮引用,然后使用。删除方法您有许多按钮要删除还是只有一个按钮?如果有多个按钮,您想如何选择要删除的按钮?我有多个按钮…我想标记我要删除的新按钮,然后使用keybord by prees delete将其删除..您有多个按钮要删除还是只有一个按钮?如果有多个按钮,您想如何选择要删除的按钮?我有多个按钮…我想标记要删除的新按钮,然后使用keybord by prees delete将其删除..我需要做什么来删除新按钮?在Visual Studio中,在窗体的设计器中,将新按钮拖动到窗体中,将其命名为removeButton,并将removingButton_Click方法附加到其Click事件。编译并运行应用程序后,您将获得一个删除按钮,该按钮允许您从动态添加的按钮中删除按钮。tnx非常有助于我:您可能拥有如何删除所需按钮而不是最后一个按钮的代码?您希望如何选择要删除的按钮?我需要做些什么来删除新按钮?在Visual Studio中Studio,在窗体的设计器中,将新按钮拖动到窗体中,将其命名为removeButton,并将removingButton_-Click方法附加到其单击事件。编译并运行应用程序后,您将获得一个删除按钮,该按钮允许您从动态添加的按钮中删除按钮。tnx非常有助于我:您可能拥有如何删除所需按钮而不是最后一个按钮的代码?您希望如何选择要删除的按钮?右键单击您的表单并选择“属性”和“删除”属性窗口转到事件并查找键关闭事件。双击该事件。它甚至会生成键关闭。在生成的事件代码中,将“窗口\按键”事件内容置于上方。右键单击窗体并选择“属性”,然后在“属性”窗口中转到“事件”并查找按键事件。双击该事件。它甚至会生成键关闭。在生成的事件代码中,将“窗口\按键”事件内容置于上方。