Results 1 to 11 of 11

Thread: {Resolved} - Killing images ... file disposal - access probs?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    {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:
    1. Dim sCurrFile As String
    2.         sCurrFile = cmbDesigns.Text
    3.         If sCurrFile.Trim = "" Then
    4.             Exit Sub
    5.         End If
    6.  
    7.         If MessageBox.Show("Remove from gallery?", "Remove image.", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK Then
    8.             Dim i As Integer = 0
    9. removeanother:
    10.             For i = 0 To Me.Controls.Count - 1
    11.                 If Me.Controls(i).GetType.ToString = "System.Windows.Forms.PictureBox" Then
    12.                     If Me.Controls(i).Name <> "pbPreview1" Then
    13.                         If Me.Controls(i).Name <> "pbPrevTes" Then
    14.                             Dim oControl As PictureBox
    15.                             oControl = CType(Me.Controls(i), PictureBox)
    16.                             oControl.Image = Nothing
    17.                             Me.Controls.Remove(oControl)
    18.                             GoTo removeanother
    19.                         End If
    20.                     End If
    21.                 End If
    22.             Next i
    23.             pbPreview1.Image = Nothing
    24.             pbPrevTes.Image = Nothing
    25.  
    26.             On Error Resume Next
    27.             Kill(Application.StartupPath & "\Gallery\KeyPatterns\" & sCurrFile.Trim)
    28.             On Error GoTo 0
    29.  
    30.             Dim sFirstFile As String = ""
    31.             sFirstFile = PopulateItems()
    32.             cmbDesigns.Text = sFirstFile
    33.             If sFirstFile <> "" Then
    34.                 Dim oBMP As New Bitmap(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(sFirstFile))
    35.                 'oBMP = ResizeImage(oBMP, 320, 600)
    36.                 UpdateImage(oBMP)
    37.             End If
    38.         End If
    Last edited by illskills; Jan 1st, 2006 at 10:29 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim MyRemoveList As New ArrayList 'list to hold the controls that need to be removed
    2.         For Each ctrl As Control In Me.Controls
    3.             If TypeOf ctrl Is PictureBox Then
    4.                 If ctrl.Name <> "pbPreview1" Or ctrl.Name <> "pbPrevTes" Then
    5.                     MyRemoveList.Add(ctrl) 'adds the control to the list
    6.                 End If
    7.             End If
    8.         Next
    9.         For Each ctrl As Control In MyRemoveList
    10.             Me.Controls.Remove(ctrl) 'removes all of the controls in the list from the form
    11.         Next
    12.  
    13. 'and the above should replace your code below...
    14. Dim i As Integer = 0
    15. removeanother:
    16.             For i = 0 To Me.Controls.Count - 1
    17.                 If Me.Controls(i).GetType.ToString = "System.Windows.Forms.PictureBox" Then
    18.                     If Me.Controls(i).Name <> "pbPreview1" Then
    19.                         If Me.Controls(i).Name <> "pbPrevTes" Then
    20.                             Dim oControl As PictureBox
    21.                             oControl = CType(Me.Controls(i), PictureBox)
    22.                             oControl.Image = Nothing
    23.                             Me.Controls.Remove(oControl)
    24.                             GoTo removeanother
    25.                         End If
    26.                     End If
    27.                 End If
    28.             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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.:
    VB Code:
    1. oControl.Image.Dispose()

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Killing images ... file disposal - access probs?

    Quote 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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Killing images ... file disposal - access probs?

    should increase the speed of my preview redraw n e ways

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: {Resolved} - Killing images ... file disposal - access probs?

    here's the code i added btw


    VB Code:
    1. Dim sCurrFile As String
    2.         sCurrFile = cmbDesigns.Text
    3.         If sCurrFile.Trim = "" Then
    4.             Exit Sub
    5.         End If
    6.  
    7.         If MessageBox.Show("Remove from gallery?", "Remove image.", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK Then
    8.             Dim MyRemoveList As New ArrayList 'list to hold the controls that need to be removed
    9.             Dim i As Integer = 0
    10.             For Each ctrl As Control In Me.Controls
    11.                 If TypeOf ctrl Is PictureBox Then
    12.                     If ctrl.Name <> "pbPreview1" Then
    13.                         If ctrl.Name <> "pbPrevTes" Then
    14.                             MyRemoveList.Add(ctrl) 'adds the control to the list
    15.                         End If
    16.                     End If
    17.                 End If
    18.             Next
    19.             For Each ctrl As Control In MyRemoveList
    20.                 Dim MyPb As PictureBox
    21.                 MyPb = ctrl
    22.                 MyPb.Image.Dispose()
    23.                 MyPb.Image = Nothing
    24.                 Me.Controls.Remove(ctrl) 'removes all of the controls in the list from the form
    25.             Next
    26.             pbPreview1.Image.Dispose()
    27.             pbPrevTes.Image.Dispose()
    28.             pbPreview1.Image = Nothing
    29.             pbPrevTes.Image = Nothing
    30.  
    31.  
    32.             On Error Resume Next
    33.             Kill(Application.StartupPath & "\Gallery\KeyPatterns\" & sCurrFile.Trim)
    34.             On Error GoTo 0
    35.  
    36.             Dim sFirstFile As String = ""
    37.             sFirstFile = PopulateItems()
    38.             cmbDesigns.Text = sFirstFile
    39.             If sFirstFile <> "" Then
    40.                 If Dir(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(cmbDesigns.Text)) = "" Then Exit Sub
    41.                 Dim oBMP As New Bitmap(Application.StartupPath & "\Gallery\KeyPatterns\" & Trim(sFirstFile))
    42.                 UpdateImage(oBMP)
    43.             End If

    all working now anyways many thanks .... will replace code at the bottom when i get chance

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim ctl As Control
    2.  
    3. For i As Integer = Me.Controls.Count - 1 To 0 Step -1
    4.     ctl = Me.Controls(i)
    5.  
    6.     If TypeOf ctl Is PictureBox AndAlso ctl.Name <> "pbPreview1" AndAlso ctl.Name <> "pbPrevTes" Then
    7.         DirectCast(ctl, PictureBox).Image.Dispose()
    8.         Me.Controls.Remove(ctl)
    9.     End If
    10. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    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 ?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    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
  •  



Click Here to Expand Forum to Full Width