【问题标题】:how to delete a button run time?如何删除按钮运行时间?
【发布时间】:2012-04-21 19:17:33
【问题描述】:

我的 C# winform 项目有问题。
在我的项目中,我有在运行时创建一个新按钮的功能。因为有时我制作了太多按钮,所以我想编写一个函数来删除我想在运行时删除的按钮。 可能有人已经有这个功能了?

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);
}

这就是我在运行时制作按钮的方式。

【问题讨论】:

  • 您有很多按钮要删除还是只有一个?如果有很多,你想如何选择删除哪一个?
  • 我有很多按钮...我想标记我要删除的新按钮,然后用 prees delete 的键盘将其删除..

标签: c# winforms button


【解决方案1】:

要删除您添加的最后一个按钮,您可以使用以下内容:

//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);
    }
}

这应该允许您删除任意数量的按钮,方法是始终删除现有按钮中添加的最后一个按钮。

更新

如果您希望能够将鼠标光标悬停在某个按钮上,然后使用 Delete 键将其删除,则可以使用此解决方案:

  • KeyPreview 设置为true,以便Form 可以接收在其控件中发生的关键事件
  • 添加buttonsAdded 列表并修改button2_Click,如本答案中描述的第一个解决方案

  • 为您的 Form 创建 KeyDown 事件处理程序并添加此代码:

    private void MySampleForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            //get control hovered with mouse
            Button buttonToRemove = (this.GetChildAtPoint(this.PointToClient(Cursor.Position)) as Button);
            //if it's a Button, remove it from the form
            if (buttonsAdded.Contains(buttonToRemove))
                {
                    buttonsAdded.Remove(buttonToRemove);
    
                    this.Controls.Remove(buttonToRemove);
                }
        }
    }
    

【讨论】:

  • 删除新按钮需要做什么?
  • 在 Visual Studio 中,在表单设计器中拖动添加一个新按钮到表单,将其命名为 removeButton 并将 removingButton_Click 方法附加到其 Click 事件。编译并运行应用程序后,您将获得一个删除按钮,允许您从动态添加的按钮中删除一个按钮。
  • tnx 真的对我有帮助:)
  • 你可能有我如何删除我想要的按钮而不是我制作的最后一个按钮的代码?
  • 您想如何选择要删除的按钮?
【解决方案2】:

你应该可以使用 this.Controls.Remove(myText);

【讨论】:

  • 您是否要通过另一个按钮的操作来删除它?你是如何尝试实现它的?
  • 不,我想标记 imade 的新按钮,然后通过在键盘上删除按钮将删除...
  • 查看 UnhandledException 的回答。将按钮引用放入类级别变量中,或者由于您要创建许多按钮,因此将按钮字典作为类级别变量,然后您可以从字典中按名称获取动态创建按钮的引用,然后使用 .Remove方法
【解决方案3】:
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();
      )
}

如果你想在删除按键时删除,你可以使用如下的按键事件

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();
          )
    }
}

【讨论】:

  • 右键单击您的表单并选择属性,然后在属性窗口中转到事件并找到按键事件。双击该事件。它甚至会为按键生成。将生成的事件代码中的Window_KeyDown事件内容放在上面。
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 2015-06-21
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多