Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:57:44+00:00 2026-06-02T09:57:44+00:00

I have a problem in my C# winform project. In my project I have

  • 0

I have a problem in my C# winform project.
In my project I have function that make a new button at runtime. Because sometimes I make too many buttons I want to write a function that deletes the button that I want to delete at run time.
Someone maybe have already that function?

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

that is how I make the button at runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:57:45+00:00Added an answer on June 2, 2026 at 9:57 am

    In order to remove the last button you’ve added, you can use something like this:

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

    This should allow you to remove as many buttons as you want by removing always the last one added from the existing ones.

    Update

    If you want to be able to hover a button with your mouse cursor and then delete it with Delete key, you can use this solution:

    • set KeyPreview to true, so Form can receive key events occured in its controls
    • add buttonsAdded list and modify button2_Click as in the first solution described in this answer

    • create KeyDown event handler for your Form and add this code ot it:

      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);
                  }
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with my C# winform project. I have function that draw squares:
I have a winform project that is using a devexpress layout control and I
i am new in project for company that have bigger enterprise/accounting system based on
I have a WinForm project that I want to be usable as a console
I have a problem with winform application which uses threads to update the UI.
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have problem in some JavaScript that I am writing where the Switch statement
I have a project where a user for the company will use a winform
I have a Windows Service project designed to monitor an email account and make
Project: Winforms, .NET framework: 3.5 (Visual Studio 2008) My problem is: I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.