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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:15:44+00:00 2026-05-24T19:15:44+00:00

I have a user control that I’m adding at runtime to a panel. There

  • 0

I have a user control that I’m adding at runtime to a panel. There is an add button to keep adding the same user control to the panel. My question is what is the best way to clear the user controls out (properly disposing of them) when you save the form or clear the form? I want to delete all additional user controls that were added from the add button and clear the first one.

  • 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-05-24T19:15:45+00:00Added an answer on May 24, 2026 at 7:15 pm

    The Control.ControlCollection.Remove method will do the trick. Run this on your user controls’ container.

    For example, if your user control’s Type is YourUserControlType and the container from which you want to remove all instances of your user control is a Panel called panel1, this code should work:

    var controlList = new List<YourUserControlType>(panel1.Controls.OfType<YourUserControlType>());
    controlList.ForEach(c => {
        panel1.Controls.Remove(c);
    });
    

    EDIT: for .NET 2.0+

    List<YourUserControlType> controlList = new List<YourUserControlType>();
    for (int i = 0; i < panel1.Controls.Count; i++) {
        YourUserControlType uc = panel1.Controls[i] as YourUserControlType;
        if (uc != null) {
            controlList.Add(uc);
        }
    }
    
    foreach (YourUserControlType uc in controlList) {
        panel1.Controls.Remove(uc);
    }
    

    Notice in both versions we’re avoiding removing controls from the controls collection as we’re iterating through that collection. Unless you’re careful, removing items from the collection as you’re iterating through can causes problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control that I've created, however when I go to add
I have a user control that takes a several seconds to load. Is there
I have a User Control that I need to programmatically add to a Silverlight
I have a user control that I'm adding to a webpage dynamically. The ascx
I have a user control that has a text box and a button. I
I have a user control that has button whose click event handler contains the
I have a user control that is pretty basic. It contains several TextBox controls,
I have a user control that I'm building. It's purpose is to display the
i have a user control that descends from UserControl. When dropped onto the form
I have a user control that has a few public properties, one is an

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.