|
-
Jan 1st, 2006, 08:45 PM
#1
Thread Starter
Lively Member
{Resolved} - Killing images ... file disposal - access probs?
i have this code below to remove dynamically created image boxes from a form and then kill the file and refresh the image list and repopulate image boxs but its not working .... keeps telling me that the files are open/readonly
is there a quick way to close these images ?
many thanks
illskills
VB Code:
Dim sCurrFile As String
sCurrFile = cmbDesigns.Text
If sCurrFile.Trim = "" Then
Exit Sub
End If
If MessageBox.Show("Remove from gallery?", "Remove image.", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK Then
Dim i As Integer = 0
removeanother:
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).GetType.ToString = "System.Windows.Forms.PictureBox" Then
If Me.Controls(i).Name <> "pbPreview1" Then
If Me.Controls(i).Name <> "pbPrevTes" Then
Dim oControl As PictureBox
oControl = CType(Me.Controls(i), PictureBox)
oControl.Image = Nothing
Me.Controls.Remove(oControl)
GoTo removeanother
End If
End If
End If
Next i
pbPreview1.Image = Nothing
pbPrevTes.Image = Nothing
On Error Resume Next
Kill(Application.StartupPath & "\Gallery\KeyPatterns\" & sCurrFile.Trim)
On Error GoTo 0
Dim sFirstFile As String = ""
sFirstFile = PopulateItems()
cmbDesigns.Text = sFirstFile
If sFirstFile <> "" Then
Dim oBMP As New Bitmap(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(sFirstFile))
'oBMP = ResizeImage(oBMP, 320, 600)
UpdateImage(oBMP)
End If
End If
Last edited by illskills; Jan 1st, 2006 at 10:29 PM.
-
Jan 1st, 2006, 09:43 PM
#2
Re: Killing images ... file disposal - access probs?
Well I don't like how you are using the goto statements, and not sure if that is even removing everything it should, try something like this:
VB Code:
Dim MyRemoveList As New ArrayList 'list to hold the controls that need to be removed
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is PictureBox Then
If ctrl.Name <> "pbPreview1" Or ctrl.Name <> "pbPrevTes" Then
MyRemoveList.Add(ctrl) 'adds the control to the list
End If
End If
Next
For Each ctrl As Control In MyRemoveList
Me.Controls.Remove(ctrl) 'removes all of the controls in the list from the form
Next
'and the above should replace your code below...
Dim i As Integer = 0
removeanother:
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).GetType.ToString = "System.Windows.Forms.PictureBox" Then
If Me.Controls(i).Name <> "pbPreview1" Then
If Me.Controls(i).Name <> "pbPrevTes" Then
Dim oControl As PictureBox
oControl = CType(Me.Controls(i), PictureBox)
oControl.Image = Nothing
Me.Controls.Remove(oControl)
GoTo removeanother
End If
End If
End If
Next i
I think you were having the same problem I was in testing it when removing a control, it messes up the remove loop because all the control indexes change, which is why I think you did the goto, in order to do it again for all the controls it missed. So the above code makes a list of controls from the controls that need to be removed, then removes them after the list is made...
Not sure if this will effect anything in the program, but it could if your controls actually arent being removed like they should be.
I also dont like the Kill command... you should not have to use it at all...
Last edited by gigemboy; Jan 1st, 2006 at 09:48 PM.
-
Jan 1st, 2006, 09:47 PM
#3
Re: Killing images ... file disposal - access probs?
Setting the Image property of a PictureBox to Nothing merely removes the displayed Image object from the PictureBox, but the Image object still exists, thus locking the file. Here's a quote from the help topic for the Bitmap constructor you are using:
The file remains locked until the Bitmap object is disposed.
This means that you have to call the Dispose method of the Bitmap object, which you can do via the Image property of the PictureBox, i.e.:
A few notes on your code.
1. I strongly suggest that you learn to use structured exception handling (SEH), i.e. Try...Catch...Finally...End Try blocks. On Error Resume Next should not be used in VB.NET.
2. You should try to avoid using GoTo statements if at all possible. There are very few places where GoTo statements are necessary and your "GoTo removeanother" is very BAD programming. What you are doing is restarting the loop every time you remove a PictureBox, thus examining all the controls you've just looked at again. If you had 100 controls and the last 10 were PictureBoxes, your code would examine the first 90 controls 10 times each. What you should be doing is reversing your loop, i.e.
VB Code:
For i = Me.Controls.Count - 1 To 0 Step -1
and get rid of that GoTo altogether. When ever you want to remove items froma collection in a loop you should always work backwards, so that those items yet to be examined do not have their indices affected by removing an item.
-
Jan 1st, 2006, 09:51 PM
#4
Re: Killing images ... file disposal - access probs?
 Originally Posted by jmcilhinney
When ever you want to remove items froma collection in a loop you should always work backwards, so that those items yet to be examined do not have their indices affected by removing an item.
Just now learned that myself 
***EDIT - and for IllSkill - we aren't "picking" on you, just showing better ways of doing it I was coding like that not too long ago, until joining this forum. I can't tell you how much I've learned in just a few months of reading the posts on here. Keep up the work, and keep reading this forum
Last edited by gigemboy; Jan 1st, 2006 at 10:16 PM.
-
Jan 1st, 2006, 10:27 PM
#5
Thread Starter
Lively Member
Re: Killing images ... file disposal - access probs?
nice one people .... im sorry bout the goto statements ... will re examin the code and get back to you with a result [ ]
thanks again
illskills
-
Jan 1st, 2006, 10:28 PM
#6
Thread Starter
Lively Member
Re: Killing images ... file disposal - access probs?
should increase the speed of my preview redraw n e ways
-
Jan 1st, 2006, 10:58 PM
#7
Thread Starter
Lively Member
Re: {Resolved} - Killing images ... file disposal - access probs?
here's the code i added btw
VB Code:
Dim sCurrFile As String
sCurrFile = cmbDesigns.Text
If sCurrFile.Trim = "" Then
Exit Sub
End If
If MessageBox.Show("Remove from gallery?", "Remove image.", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK Then
Dim MyRemoveList As New ArrayList 'list to hold the controls that need to be removed
Dim i As Integer = 0
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is PictureBox Then
If ctrl.Name <> "pbPreview1" Then
If ctrl.Name <> "pbPrevTes" Then
MyRemoveList.Add(ctrl) 'adds the control to the list
End If
End If
End If
Next
For Each ctrl As Control In MyRemoveList
Dim MyPb As PictureBox
MyPb = ctrl
MyPb.Image.Dispose()
MyPb.Image = Nothing
Me.Controls.Remove(ctrl) 'removes all of the controls in the list from the form
Next
pbPreview1.Image.Dispose()
pbPrevTes.Image.Dispose()
pbPreview1.Image = Nothing
pbPrevTes.Image = Nothing
On Error Resume Next
Kill(Application.StartupPath & "\Gallery\KeyPatterns\" & sCurrFile.Trim)
On Error GoTo 0
Dim sFirstFile As String = ""
sFirstFile = PopulateItems()
cmbDesigns.Text = sFirstFile
If sFirstFile <> "" Then
If Dir(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(cmbDesigns.Text)) = "" Then Exit Sub
Dim oBMP As New Bitmap(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(sFirstFile))
UpdateImage(oBMP)
End If
all working now anyways many thanks .... will replace code at the bottom when i get chance
-
Jan 1st, 2006, 11:48 PM
#8
Re: {Resolved} - Killing images ... file disposal - access probs?
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:
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
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.
-
Jan 2nd, 2006, 12:22 AM
#9
Thread Starter
Lively Member
Re: {Resolved} - Killing images ... file disposal - access probs?
thanks jm ... ill add the above function mate
once again cheers for the know how 
how does the io.file.delete() cmd differ from kill ?
and whats the correct syntax ?
-
Jan 2nd, 2006, 02:03 AM
#10
Re: {Resolved} - Killing images ... file disposal - access probs?
The correct syntax is whatever it says in the help/MSDN:
Kill
IO.File.Delete
Kill is useful as it can accept wildcards, but for a specific file I'd always use File.Delete as Kill will call File.Delete anyway. Also, File.Delete does not throw an exception if the file does not exist, while Kill does.
-
Jan 2nd, 2006, 12:59 PM
#11
Thread Starter
Lively Member
Re: {Resolved} - Killing images ... file disposal - access probs?
ahhhhh ncie one
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|