简体   繁体   中英

Delete dynamically created controls

I keep getting stuck on this part of my program. whenver i call an listbox.selectitemchange event, i want the proper amount of trackbar and labels to be displayed.

now, it does not work properly. Some of them get removed when the event is called, some of them don't.

foreach (Label label in Controls.OfType<Label>())
{
    if (label.Tag != null && label.Tag.ToString() == "dispose")
    {
        label.Dispose();
    }
}

foreach (TrackBar trackBar in Controls.OfType<TrackBar>())
{
    if (trackBar.Tag != null && trackBar.Tag.ToString() == "dispose")
    {
        trackBar.Dispose();
    }
}


    for (int i = 0; i < calc; i++)
    {
        //string[] LineWidthSplitted = lines[lineWidth].Split(' ');
        //Int32.TryParse(LineWidthSplitted[2], out WidthValue);


        Label Label = new Label();
        Label.Name = "TrackbarWidth" + LabelName++;
        Label.Tag = "dispose";
        Label.Text = "Board -" + LabelName + "- Height:";
        Label.Location = new Point(10, 450 + (50 * LabelName));
        Label.Size = new System.Drawing.Size(100, 25);
        this.Controls.Add(Label);



        TrackBar trackBar = new TrackBar();
        trackBar.Name = "TrackbarWidth" + trackbarName++;
        trackBar.Tag = "dispose";
        trackBar.Maximum = 85;
        trackBar.Minimum = 65;
        trackBar.SmallChange = 5;
        trackBar.TickFrequency = 5;
        trackBar.Value = 65;
        trackBar.Location = new Point(150, 450 + (50 * trackbarName));
        trackBar.Size = new System.Drawing.Size(100, 25);
        this.Controls.Add(trackBar);


        lineWidth += 4;

    }

while, when i remove the foreach for the trackbar, all labels get properly displayed. they all get deleted, en recreated for the pricese amount needed to be created, no exceptions.

Any reason why?

thank you.

Don't use "Dispose" on the labels right away. First remove them. Note that you can't modify the Controls collection inside the foreach so you have to do something like this:

List<Label> itemsToRemove = new List<Label>();
foreach (Label label in Controls.OfType<Label>())
{
    if (label.Tag != null && label.Tag.ToString() == "dispose")
    {
        itemsToRemove.Add(label);
    }
}

foreach (Label label in itemsToRemove)
{
    Controls.Remove(label);
    label.Dispose();
}

If you want to remove all different kinds of controls in one swoop:

List<Control> itemsToRemove = new List<Control>();
foreach (Control ctrl in Controls)
{
    if (ctrl.Tag != null && ctrl.Tag.ToString() == "dispose")
    {
        itemsToRemove.Add(ctrl);
    }
}

foreach (Control ctrl in itemsToRemove)
{
    Controls.Remove(ctrl);
    ctrl.Dispose();
}

I can't test this now, but I think you should also remove the controls from the Form Controls collection where you have added them. By the way, in your case I think you could avoid the OfType extension and use the old fashioned for..loop that will allow to execute just one loop....

for(int x = this.Controls.Count - 1; x >= 0; x--))
{
    Control ctr = this.Controls[x]; 
    if (ctr Is Label && ctr.Tag != null && ctr.Tag.ToString() == "dispose")
    {
        this.Controls.Remove(ctr);
        ctr.Dispose();
    }
    if(ctr Is TrackBar && ctr.Tag != null && ctr.Tag.ToString() == "dispose")
    {
        this.Controls.Remove(ctr);
        ctr.Dispose();
    }
}

Notice how removing elements from a collection with a for..loop should be done in reverse order, from the end to start of the collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM