Problems removing a control

  • Thread starter Thread starter Nightmare
  • Start date Start date
N

Nightmare

Guest
Hey there. Im wondering why this code is not working for me:

Code:
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:
 
You cant change the contents of a collection while enumerating it. Instead, try something like this:

Code:
        Dim intIndex As Integer

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

Duuuuh! I dont know what I was thinking. Works perfectly now. Thanx a lot Divil.
 
Back
Top