I don't quite understand why you would choose to use two loops to do something that I gave you a perfectly good solution to that uses a single loop:Setting the Image property of the PictureBoxes being removed does nothing useful. You only need to cast the Control to a PictureBox in order to access its Image property once to Dispose it. I also suggest using the IO.File.Delete method instead of Kill.VB Code:
Dim ctl As Control For i As Integer = Me.Controls.Count - 1 To 0 Step -1 ctl = Me.Controls(i) If TypeOf ctl Is PictureBox AndAlso ctl.Name <> "pbPreview1" AndAlso ctl.Name <> "pbPrevTes" Then DirectCast(ctl, PictureBox).Image.Dispose() Me.Controls.Remove(ctl) End If Next i




Reply With Quote