views:

407

answers:

4

I have a program that adds a series of "blips" to a graph. How do I have a button clear all of the "blips" that have been created with this code?

                        PictureBox blip = new PictureBox();
                        blip.Location = new Point(blipHours, blipAltitude);
                        blip.Size = new Size(6, 6);
                        blip.BackColor = System.Drawing.Color.Lime;
                        blip.Text = "";
                        blip.Name = callsign;
                        this.Controls.Add(blip);
                        this.Controls.SetChildIndex(blip, 0);

The other part of my question: See that I have named each blip with "callsign." Is there a way to change a blip's background color when it's name is equal to a certain callsign ? Each blip is associated with a selection in a listbox, and I would like have it change the blips color when that user selects it.

+1  A: 
this.Controls.Clear();
JonathanK
Optionally, because the controls are dynamically created, the "Clear" button could simply just rebuild the control on a full postback with nothing in it.
Chris Lively
this will remove all of the controls, regardless of whether they are PictureBox or not.
Stan R.
this clears all controls. I just want to clear the "blip" controls that were created.
Brodie
Yes the context of 'this' in the example is unclear. The solution Stan has posted seems fitting for the context that is now implied. You may consider wrapping this in a UserControl, in which case removing all controls from the this.Controls collection with the Clear() method becomes viable.
JonathanK
+1  A: 

You might want to add the blip to a List and then when the user clicks the "Clear" button, just iterate over the list, remove the blip from the Controls collection, then clear the list.

In terms of changing the background color, why don't you just use an if statement?

blip.BackColor = callsign == "SpecialSign"? System.Drawing.Color.Red : System.Drawing.Color.Lime
statichippo
Jonathan Keith mentioned this.Controls.Clear() which may work, but depending on the context (of "this"), might clear other controls too. If the context permits, however, Jonathan's answer may take a couple less clock cycles.
statichippo
+1  A: 

This will remove all of the PictureBox controls from the particular container (i assume a graph in your case).

 for (int i = this.Controls.Count - 1; i >= 0; i--)
            {
                PictureBox control = this.Controls[i] as PictureBox;
                if (control == null)
                    continue;

                control.Dispose();
            }
Stan R.
+5  A: 

Everybody is forgetting a very important detail: you have to Dispose() the control or it will leak forever:

for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
  if (this.Controls[ix] is PictureBox) this.Controls[ix].Dispose();
Hans Passant
+1..thanks for that, completely forgot. in fact controls disposing method calls parent.Controls.Remove(this)
Stan R.
It won't leak forever... it will just leak until the Garbage collector comes around... and then hopefully the destructor for PictureBox includes a call to Dispose.
Nick
@Stan R - It does? Cool... I did not know that!
Nick
It doesn't. Controls are kept alive by their Handle property. The native window will stay around, it just isn't visible.
Hans Passant
@nobugz, it doesn't what?
Stan R.
+1, and `Dispose` will also automatically remove the control from the container's control collection (that's why the list iterates down). Btw, you answered this even better here: http://stackoverflow.com/questions/1969705/clear-controls-does-not-dispose-them-what-is-the-risk/1970158#1970158 ;-)
0xA3
@divo: couldn't use it, only picture boxes were to be removed.
Hans Passant
@stan: it doesn't == garbage collector won't clean it up, the control is referenced.
Hans Passant
@nobugz, oh ok. i thought you were saying it doesn't remove itself from the parent.
Stan R.