Jump to content
Xtreme .Net Talk

Problems removing a control


Recommended Posts

Guest Nightmare
Posted

Hey there. I'm wondering why this code is not working for me:

 

Dim pic As Control

For Each pic In panContainer.Controls
   panContainer.Controls.Remove(pic)
Next pic

 

It seems as if only some of the pictures within panContainer are being removed. If I repeated this code again a few more of the pictures in panContainer will be removed. I have no idea what is going on. How can I completely remove any controls which might exist within panContainer? Any hints you could give me would be appreciated :cool:

  • *Gurus*
Posted

You can't change the contents of a collection while enumerating it. Instead, try something like this:

 

       Dim intIndex As Integer

       For intIndex = Panel1.Controls.Count - 1 To 0 Step -1
           Panel1.Controls.Remove(Panel1.Controls(intIndex))
       Next

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Guest Nightmare
Posted

Thank you

 

Duuuuh! I don't know what I was thinking. Works perfectly now. Thanx a lot Divil.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...