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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:57+00:00 2026-05-26T11:16:57+00:00

I have dynamically add Label on FlowLayoutPanel , with the following code: private void

  • 0

I have dynamically add Label on FlowLayoutPanel, with the following code:

private void button1_Click(object sender, EventArgs e)
    {
        Label lb = new Label();
        lb.Text = "How are You";
        lb.Size = new Size(650, Font.Height +10);
        flowLayoutPanel1.Controls.Add(lb);
        flowLayoutPanel1.SetFlowBreak(lb, true);
        lb.BackColor = Color.Wheat;
    }

In ContextMenuStrip I have add two Item Add and Edit and associate it FlowLayoutPanel, mean when user right click on the FlowLayoutPanel, Edit and Remove menu appears.

Now I want to delete the dynamically add Label using remove button (ContextMenuStrip ). I want to just right click on the desire lebel and after right click it should be removed. and the same case with Edit button for edit.

  • 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-26T11:16:57+00:00Added an answer on May 26, 2026 at 11:16 am

    keep a reference to your lb variable around on the form (instead of just inside the function). When you want to remove it, call flowLayoutPanel1.Controls.Remove(lb).

    You should add an event handler to the label in the same sub where it is called for the label’s right click event. Inside this handler is where the above call to .Remove should be.

    Alternatively, since the event handler will pass the sender object in, which will be reference to the control that the event fired on, you can just call .Remove and pass in sender. you won’t have to keep a reference to the label around this way unless you need it for something else.

    Requested Example

    flowLayoutPanel1.Controls.Remove((ToolStripMenuItem) sender);
    

    Edited again after comments

    I changed your button1’s click event to

    private void button1_Click(object sender, EventArgs e)
    {
         lb = new Label();
        lb.Text = "How are You";
        lb.Size = new Size(650, Font.Height +10);
        flowLayoutPanel1.Controls.Add(lb);
        flowLayoutPanel1.SetFlowBreak(lb, true);
        lb.BackColor = Color.Wheat;
        lb.MouseEnter += labelEntered;
    }
    

    as you can see I added a MouseEntered event handler to catch the last label that the mouse was over.

    I added the following sub, which is handler mentioned above. All it does it record the last label that the mouse was over.

    private Label lastLabel;
    private void labelEntered(object sender, EventArgs e)
    {
        lastLabel = (Label)sender;
    }
    

    The code for the remove button has been changed to this.

    public void Remove_Click(object sender, EventArgs e)
    {
        if (lastLabel != null)
        {
            flowLayoutPanel1.Controls.Remove(lastLabel);
            lastLabel = null;
        }
    }
    

    First thing it does it check to make sure that lastLabel has a value, if it does it removes the last label that the mouse was over, then clears the lastLabel variable.

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

Sidebar

Related Questions

I have this following where I would like to dynamically add the list elements
I have a TabBar Control with 3 tab, i need to add controls(Button,Label) dynamically
I have a GridView that I need to dynamically add TemplateField elements to. My
I have a page where the user can dynamically add file upload boxes. Adding
I have a table of data that I need to dynamically add a column
I have a gridview which is databound, I want to dynamically add a row
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I'm attempting to dynamically add both columns and rows to a TableLayoutPanel. I have
In WPF C#, in code behind, I have to dynamically create an Array of

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.