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 8984263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:03:27+00:00 2026-06-15T21:03:27+00:00

I have dynamically generated controls on the panels of windows form and i have

  • 0

I have dynamically generated controls on the panels of windows form and i have also generated a button for removing the controls, all in rows.

int c = 0;
private void button1_Click(object sender, EventArgs e)
{
    int v;
    v = c++;
    panel1.VerticalScroll.Value = VerticalScroll.Minimum;
    ComboBox combo = new ComboBox();
    combo.Name = "combobox" + v ;
    combo.Location = new Point(30, 5 + (30 * v));

    ComboBox combo2 = new ComboBox();
    combo2.Name = "combobox2" + v ;
    combo2.Location = new Point(170, 5 + (30 * v));

    TextBox txt = new TextBox();
    txt.Name = "txtbx" + v;
    txt.Location = new Point(300, 5 + (30 * v));

    TextBox txt2 = new TextBox();
    txt2.Name = "txtbx2" + v;
    txt2.Location = new Point(450, 5 + (30 * v));

    TextBox txt3 = new TextBox();
    txt3.Name = "txtbx3" + v;
    txt3.Location = new Point(600, 5 + (30 * v));

    Button btn = new Button();
    btn.Name = "btn" + v;
    btn.Text = "Remove";
    btn.Location = new Point(750, 5 + (30 * v));
    panel1.Controls.Add(combo);
    panel1.Controls.Add(btn);
    panel1.Controls.Add(txt);
    panel1.Controls.Add(combo2);
    panel1.Controls.Add(txt2);
    panel1.Controls.Add(txt3);
    btn.Click += new EventHandler(btn_Click);
    combo.Tag = btn; 
    combo2.Tag = combo; 
    btn.Tag = combo2;

}

 private void btn_Click(object sender, EventArgs e)
 {
     ComboBox cb3 = btnh.Tag as ComboBox;
     ComboBox cb4 = cb3.Tag as ComboBox;
     panel1.Controls.Remove(cb3);
     panel1.Controls.Remove(cb4);
     panel1.Controls.Remove(btnh);
  }

Now how do I remove all the controls from a row upon clicking a button from that row?

  • 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-15T21:03:28+00:00Added an answer on June 15, 2026 at 9:03 pm

    You are still not saying which control you want to remove, what type of controls you want to remove or how you want to identify them.

    You could just loop through the controls to remove specific Controls.

    If you have Linq, its easy:

    private void btn_Click(object sender, EventArgs e)
    {
        panel1.Controls.Clear(); //to remove all controls
    
    
        //to remove all comboboxes
        foreach (Control item in panel1.Controls.OfType<ComboBox>().ToList())
        {
            panel1.Controls.Remove(item); 
        }
    
    
       //to remove control by Name
        foreach (Control item in panel1.Controls.OfType<Control>().ToList())
        {
            if (item.Name == "bloodyControl")
                panel1.Controls.Remove(item); 
        }
    
        
        //to remove just one control, no Linq
        foreach (Control item in panel1.Controls)
        {
            if (item.Name == "bloodyControl")
            {
                 panel1.Controls.Remove(item);
                 break; //important step
            }
        }
    }
    

    Edit:

    Its easy to do the same since you’re tagging the control already. All you need is to just retrieve the control back from tag. But you need to tag appropriately:

    Do this instead:

    private void button1_Click(object sender, EventArgs e)
    {
        int v;
        v = c++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
    
        Button btn = new Button();
        btn.Name = "btn" + v;
        btn.Text = "Remove";
        btn.Location = new Point(750, 5 + (30 * v));
        btn.Click += new EventHandler(btn_Click);
        
        ComboBox combo = new ComboBox();
        combo.Name = "combobox" + v ;
        combo.Location = new Point(30, 5 + (30 * v));
        combo.Tag = btn;
    
        ComboBox combo2 = new ComboBox();
        combo2.Name = "combobox2" + v ;
        combo2.Location = new Point(170, 5 + (30 * v));
        combo2.Tag = btn;
    
        TextBox txt = new TextBox();
        txt.Name = "txtbx" + v;
        txt.Location = new Point(300, 5 + (30 * v));
        txt.Tag = btn;
    
        TextBox txt2 = new TextBox();
        txt2.Name = "txtbx2" + v;
        txt2.Location = new Point(450, 5 + (30 * v));
        txt2.Tag = btn;
    
        TextBox txt3 = new TextBox();
        txt3.Name = "txtbx3" + v;
        txt3.Location = new Point(600, 5 + (30 * v));
        txt3.Tag = btn;
    
        panel1.Controls.Add(combo);
        panel1.Controls.Add(btn);
        panel1.Controls.Add(txt);
        panel1.Controls.Add(combo2);
        panel1.Controls.Add(txt2);
        panel1.Controls.Add(txt3);    
    }
    
    private void btn_Click(object sender, EventArgs e)
    {
       //to remove control by Name
        foreach (Control item in panel1.Controls.OfType<Control>().ToList())
        {
            if (item.Tag == sender || item == sender)
                panel1.Controls.Remove(item); 
        }
    }
    

    Here you are tagging controls with the button, hence on the button click you can remove all the controls whose tags are the clicked button which you get from sender argument. But the downside of this approach is that you have to enumerate all the controls of the panel which is not great.

    Edit: As I came to learn the below code is for a table layout panel which the OP isn’t using for now. But anyway a table panel layout is better suited for this job.

    I would suggest you to do this:

    private void button1_Click(object sender, EventArgs e)
    {
        int v;
        v = c++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
    
        Button btn = new Button();
        btn.Name = "btn" + v;
        btn.Text = "Remove";
        btn.Location = new Point(750, 5 + (30 * v));
        btn.Click += new EventHandler(btn_Click);
        btn.Tag = v;
    
        ComboBox combo = new ComboBox();
        combo.Name = "combobox" + v ;
        combo.Location = new Point(30, 5 + (30 * v));
        combo.Tag = v;
    
        ComboBox combo2 = new ComboBox();
        combo2.Name = "combobox2" + v ;
        combo2.Location = new Point(170, 5 + (30 * v));
        combo2.Tag = v;
    
        TextBox txt = new TextBox();
        txt.Name = "txtbx" + v;
        txt.Location = new Point(300, 5 + (30 * v));
        txt.Tag = v;
    
        TextBox txt2 = new TextBox();
        txt2.Name = "txtbx2" + v;
        txt2.Location = new Point(450, 5 + (30 * v));
        txt2.Tag = v;
    
        TextBox txt3 = new TextBox();
        txt3.Name = "txtbx3" + v;
        txt3.Location = new Point(600, 5 + (30 * v));
        txt3.Tag = v;
    
        panel1.Controls.Add(combo);
        panel1.Controls.Add(btn);
        panel1.Controls.Add(txt);
        panel1.Controls.Add(combo2);
        panel1.Controls.Add(txt2);
        panel1.Controls.Add(txt3);    
    }
    
    private void btn_Click(object sender, EventArgs e)
    {
        int toBeDeletedRow = (int)((Control)sender).Tag;
        for (int row = panel1.RowCount - 1; row >= 0; row--)
        {
            if (row == toBeDeletedRow)
            {
                panel1.RowStyles.RemoveAt(row);
                panel1.RowCount--;
                return;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my C# Windows Forms form I have some buttons which are dynamically generated.
Fiddle: http://jsfiddle.net/MhWm5/16/ I have some dynamically generated table rows/values with dynamic IDs <td class=control-group>
I have a page in which controls are dynamically generated as the user navigates
So I have a form that gets dynamically generated by a script to have
I have a page that contains dynamically generated Dropdown List controls and I want
I have a submit button that has got a dynamically generated id. I want
I have a form representing a survey that is dynamically generated based on some
I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have a dynamically generated table and for each row in the table there
I have a dynamically generated rss feed that is about 150M in size (don't

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.