I am using the following code in order to delete all my controls inside a FlowLayoutPanel:
While FlowLayoutPanel1.Controls.Count > 0
Dim controltoremove = FlowLayoutPanel1.Controls(0)
FlowLayoutPanel1.Controls.Remove(controltoremove)
controltoremove.Dispose()
Application.DoEvents()
End While
This seems to work (as in i no longer see my controls inside my FlowLayoutPanel1 box) but once i get to this part of the code:
My.Computer.FileSystem.DeleteFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
It tells me that the image is currently in use??? Since i disposed these controls (that have these images as backgroundimage) shouldn't i be able to delete the image file??
To create the buttons inside the FlowLayoutPanel i use this:
For Each subitem As JObject In item.Values
Dim newPictureBox As New Button
Dim Client As New WebClient
strID = "http://graph.facebook.com/" & subitem("id").ToString.Replace("""", "") & "/picture?width=126&height=114"
strName = subitem("name").ToString
Client.DownloadFile(strID, "C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
Client.Dispose()
Dim bmp As New Bitmap(Image.FromFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg"))
newPictureBox.BackgroundImage = Image.FromFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
newPictureBox.Tag = subitem("id").ToString.Replace("""", "")
newPictureBox.Name = "img" & intX
newPictureBox.Width = bmp.Width.ToString()
newPictureBox.Height = bmp.Height.ToString()
FlowLayoutPanel1.Controls.Add(newPictureBox)
AddHandler newPictureBox.Click, AddressOf newPictureBox_Click
intX += 1
imgDir.Add(strName, subitem("id").ToString.Replace("""", ""))
bmp.Dispose()
Next
And it looks like the problem is the Dim newPictureBox As New Button. If i add
newPictureBox.dispose()
at the end of that code above it works with deleting the images from the directory... but is never displays the images inside the buttons to begin when if i do that :o/
What would I be doing incorrectly?