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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:13:44+00:00 2026-06-05T05:13:44+00:00

I create a label label1 dynamically in a method. Then when I click a

  • 0

I create a label “label1” dynamically in a method. Then when I click a button I want to remove that label created but if I write Controls.Remove(label1) it says that the control doesn’t exist in the context.
How could I do to achieve this?

EDIT: Following Jon suggestion I implemented the foreach loop but it doesn’t do anything. This is my code, the panel which I use is created by design:

void GenerateControls() {
    Label labelOne = new Label();
    Button btnContinue = new Button();
    panel.SuspendLayout();
    SuspendLayout();
    //btnContinue
    btnContinue.BackColor = System.Drawing.Color.Black;
    btnContinue.ForeColor = System.Drawing.SystemColors.Menu;
    btnContinue.Location = new System.Drawing.Point(145, 272);
    btnContinue.Name = "btnContinue";
    btnContinue.Size = new System.Drawing.Size(95, 28);
    btnContinue.TabIndex = 13;
    btnContinue.Text = "Continue";
    btnContinue.Visible = true;
    Controls.Add(btnContinue);
    btnContinue.Click += new System.EventHandler(btnContinue_Click);            
    //labelOne            
    labelOne.Location = new Point(0,65); 
    labelOne.Size = new System.Drawing.Size(100,20);
    labelOne.Text = "labelOne";
    labelOne.Name = "labelOne";
    labelOne.Visible = true;
    labelOne.TextChanged += new System.EventHandler(this.lbl_TextChanged);
    labelOne.BackColor = System.Drawing.Color.PaleGreen;
    Controls.Add(labelOne);
    //panel
    panel.Controls.Add(labelOne);
    panel.Visible = true;
    panel.Location = new Point(0,0);
    panel.Size = new Size(240, 320);
    //
    Controls.Add(panel);
    panel.ResumeLayout();
    ResumeLayout();
}

And then in when I click on btnContinue:

private void btnContinuar_Click(object sender, EventArgs e) {
    foreach (Control control in panel.Controls) {
        if (control.Name == "labelOne"){
            panel.Controls.Remove(control);
            break;
        }
    }
}

I debug it and in the panel.Control it continues as if it were empty panel.
Thanks for your help!

  • 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-05T05:13:46+00:00Added an answer on June 5, 2026 at 5:13 am

    I suspect it says the variable doesn’t exist in that context. You’ll have to find the label by its text, or knowing something else about it. For example, when you create it you could set the Name property and find it by that when you want to remove it:

    panel.Controls.RemoveByKey("YourLabelName");
    

    EDIT: As noted in the comments, RemoveByKey doesn’t exist in the compact framework. So you’d either have to remember the reference yourself (in which case you don’t need the name) or use something like:

    foreach (Control control in panel.Controls)
    {
        if (control.Name == "YourLabelName")
        {
            panel.Controls.Remove(control);
            break;
        }
    }
    

    EDIT2: And to make it even more “generic” and desktop compatible, you could keep the RemoveByKey call and add this to your app:

    public static class FormExtensions
    {
        public static void RemoveByKey(this Control.ControlCollection collection,
                                       string key)
        {
            if(!RemoveChildByName(collection, key))
            {
                throw new ArgumentException("Key not found");
            }
        }
    
        private static bool RemoveChildByName(
            this Control.ControlCollection collection,
            string name)
        {
            foreach (Control child in collection)
            {
                if (child.Name == name)
                {
                    collection.Remove(child);
                    return true;
                }
    
                // Nothing found at this level: recurse down to children.
                if (RemoveChildByName(child.Controls, name))
                {
                    return true;
                }
            }
    
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how to create a label that contains two icons, one
I am trying to create a postage label that is populated dynamically in C#.
I want to add three labels into a cell dynamically, cell is created dynamically
How to create dynamic label and button in flex 3?
i have this crazy label for each time i create a migration that use
I want to create a fading effect of text in Label control. I change
What ways can you dynamically create controls in C#? This was objects at first
I'm having a problem with a Web Control that is dynamically created and inserted
When i create the text and label box dynamically it should sit in the
I have a ASP table that I create dynamically on the page load event.

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.