0

I am drawing set of Labels dynamically and i want to remove them. I am using the same button to remove and add new Labels. The labels are drawn randomly with random coordinates. But, when i am pressing the button the old Labels should me removed and the new Labels appeared. But, what I am having is that the new labels appears and old Labels appears but empty labels. I want them to be remove at all. See the picture:

enter image description here

//Global Intialization
int xCoor;
int yCoor;

//send the random method
Random coor = new Random();

private void btnRun_Click(object sender, EventArgs e) 
{
    //Removing the Labels after drawing them in the picBox
    this.RemoveOldLabels();

    //to  draw the Labels rendomely.
    for (int x = 1; x <= 25; x++)
    {
        for (int i = 0; i < 1; i++)
        {
            //Get the Coordinates for X,Y 
            xCoor = coor.Next(0, 750);
            yCoor = coor.Next(0, 500);

            //Start Greating the Labels
            Label nodeLabel1 = new Label();

            nodeLabel1.Text = x + " : " + xCoor + "," + yCoor;
            nodeLabel1.AutoSize = true;
            nodeLabel1.Location = new Point(xCoor + 10, yCoor + 5);
            nodeLabel1.ForeColor = System.Drawing.Color.Red;
            nodeLabel1.BackColor = Color.LightBlue;

            //Draw the Labels in the PicBox
            this.picNodes.Controls.Add(nodeLabel1);
        }
    }
}

//this to  remove the Labels 
private void RemoveOldLabels()
{
    List<Label> LabelsToRemove = new List<Label>();

    foreach (var x in this.picNodes.Controls)
    {
        if (x.GetType() == typeof(System.Windows.Forms.Label))
        {
            LabelsToRemove.Add((Label)x);
        }
    }
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Haitham242
  • 27
  • 7
  • Why don't you just create a list of labels and add the labels you create to the list, then in RemoveOldLabels you remove them all? – cbr May 12 '14 at 05:54
  • Because I going to have random numbers for labels later. – Haitham242 May 12 '14 at 06:00
  • I don't understand - do you mean you're going to have random numbers in their names or...? – cbr May 12 '14 at 06:01
  • possible duplicate of [Delete dynamically created controls](http://stackoverflow.com/questions/17194762/delete-dynamically-created-controls) – cbr May 12 '14 at 06:06
  • I am going to have random number of labels at every-time. – Haitham242 May 12 '14 at 06:07

2 Answers2

4

It looks like RemoveOldLabels() is finding the labels, but not actually removing them.

Try getting all "Label" controls, then disposing of them (which should also remove them from the collection and make them disappear off the form).

foreach (var label in picNodes.Controls.OfType<Label>().ToList())
    label.Dispose();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 1
    Did not realise that disposing a control automatically removes it from the parent's control collection. Thanks Grant. Found some more info on this here: http://stackoverflow.com/questions/2013877/need-i-remove-controls-after-disposing-them – Lummo May 12 '14 at 07:05
1

Your remove old labels logic does not do anything. It simply adds the labels to a list and that's it.

You need to remove those controls from picNodes as below:

  private void RemoveOldLabels()
  {
    List<Label> LabelsToRemove = new List<Label>();

    foreach (var x in this.picNodes.Controls)
    {
        if (x.GetType() == typeof(System.Windows.Forms.Label))
        {
            LabelsToRemove.Add((Label)x);
        }
    }

    foreach (var label in LabelsToRemove)
    {
        this.picNodes.Controls.Remove(label);
        label.Dispose();
    }
  }
Lummo
  • 1,159
  • 9
  • 14
  • There are more concise ways to write the code above (using LINQ) but I've reused your existing code and naming where possible. – Lummo May 12 '14 at 06:17
  • Please press the button 3 times – Haitham242 May 12 '14 at 06:28
  • They are not removing the previous Labels. – Haitham242 May 12 '14 at 06:54
  • I've just tested this and it works fine using your code with the above modifications. All the old labels are removed each time and new ones appear randomly. Not sure what else I can suggest - make sure you don't have any other code adding labels to picNodes. – Lummo May 12 '14 at 07:02