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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:03:21+00:00 2026-06-16T01:03:21+00:00

i have windows form which have a button, when button is clicked it dynamically

  • 0

i have windows form which have a button, when button is clicked it dynamically generated controls, also a button which is dynamically generated is added so that it will remove the controls which are in line, means aside the button, a row of control will be removed when button clicked my code is

int c = 0;
        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)// this is the dynamically added button's event which will remove the combobox and textbox
        {
            Button btnh = sender as Button;
            foreach (Control item in panel1.Controls.OfType<TextBox>())
            {
                if (item.Tag == sender || item == sender)
                    panel1.Controls.Remove(item);
            }
            foreach (Control item in panel1.Controls.OfType<ComboBox>())
            {
                if (item.Tag == sender || item == sender)
                    panel1.Controls.Remove(item);
            }
            panel1.Controls.Remove(btnh);
        }

my error is nothing but the problem is it dosnt removes all the controls it leaves controls and i dont know whats the problem my code is simple and easy but i dont know where it lacks

  • 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-16T01:03:22+00:00Added an answer on June 16, 2026 at 1:03 am

    The problem is that you are removing items while you are looping through the collection. The foreach-statement might skip items in that case. Store the items to be removed in a list first:

    List<Control> toBeRemoved = panel1.Controls
        .Cast<Control>()
        .Where(c => c.Tag == sender)
        .ToList();
    foreach (Control c in toBeRemoved) {
        panel1.Controls.Remove(c);
    }
    

    You can also remove in a loop easily, if you loop backwards:

    for (int i = panel1.Controls.Count - 1; i >= 0; i--) {
        if (panel1.Controls[i].Tag == sender) {
            panel1.Controls.RemoveAt(i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows form (Form1) which when a button is clicked on it
I have a form MainForm which is a Windows Forms form that contains many
I have a Windows form that's generated using code (including buttons and what not).
I have a windows-form which runs without any problem and displayed well. But if
I have a requirement to create a windows form control which has to detect
I have a windows form app, and a class which is called Game. Let's
(Modifying the question)I have a windows form app in C# which calls into some
I have a program which displays an image to the windows form, and places
Im using c# .net , windows form application. I have a XML file which
I have a C# windows application which does the following: 1) the main form

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.