Share via


Remove picture Box Controls

Question

Thursday, November 16, 2006 7:13 AM

Hi all

I have windows form, where in picture boxes are dynamically being created,
I need to Remove all picture boxes from the form every time user clicks on the button to load new images , how can i acheive this.

i tried with the following code , but it didn't worked
          
 PictureBox pic;
             foreach ( Control ctr in frmScreenShot.ActiveForm.Controls)
              {
                        if (typeof(PictureBox)== ctr.GetType())
                    {
                     
                        ctr.Dispse();
                      
                      
         }

i also tried with this code snippet
PictureBox pc;
    if (this.Controls.Contains(System.Windows.Forms.PictureBox ))
            {
               this.Controls.Remove(pc) ;
            }

Welcome Each suggestion

Thanks

All replies (7)

Thursday, November 16, 2006 7:33 AM ✅Answered

Hi,

try the following piece of code:

foreach (Control control in this.Controls)
{
    PictureBox picture = control as PictureBox;
    if (picture != null)
    {
        this.Controls.Remove(picture);
    }
}

Note that if your pictureboxes are not put directly on the form, you'll have to include their container in the code above (e.g. this.panel1.Controls and this.panel1.Controls.Remove...).

Andrej

 


Thursday, November 16, 2006 8:01 AM ✅Answered | 1 vote

You should be careful to modify the collection you are enumerating. A safer solution would be to first iterate through the controls for all pictureboxes and later remove them.

/// <summary>

/// Removes all child controls of specified type in a control

/// </summary>

/// <param name="control">Parent control</param>

/// <param name="type">Type of control to remove</param>

private void RemoveControls(Control control, Type type)

{

   List<Control> controls = new List<Control>();

 

   Stack<Control> stack = new Stack<Control>();

   stack.Push(control);

 

   while (stack.Count > 0)

   {

      Control ctrl = stack.Pop();

      foreach (Control child in ctrl.Controls)

      {

         if (child.GetType() == type)

         {

            controls.Add(child);

         }

         else if (true == child.HasChildren)

         {

            stack.Push(child);

         }

      }

   }

 

   foreach (Control ctrl in controls)

   {

      control.Controls.Remove(ctrl);

      ctrl.Dispose();

   }

}

 

Can be called like this to remove all picturebox controls on the current form
RemoveControls(this, typeof(PictureBox));

 


Thursday, November 16, 2006 7:39 AM

Thanks in deed , it worked fine
i stand graceful
bye


Thursday, November 16, 2006 8:10 AM

Great !

well this is much better, but if the scenerio is like this , that i want all picture box controls to be removed Except one for which the name property is set to "xxx".

Welcome ur getback

Thanks


Thursday, November 16, 2006 9:37 AM

You could add another parameter to the method that gives the name of the control to exclude. That is the most simple approach.

You can then either place a condition on where the control gets added to the collection to remove or where it is actually removing it.

private void RemoveControls(Control control, Type type, string excludeControl)
...
if (child.GetType() == type)
{
   if (child.Name != excludeControl)
   {
      controls.Add(child);
   }
}
...


Thursday, November 16, 2006 10:00 AM

Cheers !

When i tried to execute the code u gave me earlier, i am getting the following Error
....
List<Control> controls = new List<Control>();
Stack<Control> stack = new Stack<Control>();              
.....
A new expression requires () or [] after type            // Error Message i am getting

I m using Visual studio 2003 with Framework 1.1  , it is not allowing me to declare above two statements.

Thanks in Advance


Thursday, November 16, 2006 10:07 AM

The code is for .NET 2.0 but here is to change it to work on 1.1.

Change the lines you mention to
ArrayList controls = new ArrayList();
Stack stack = new Stack();

Change this line
Control ctrl = stack.Pop();

to
Control ctrl = stack.Pop() as Control;