Results 1 to 5 of 5

Thread: List View losing images on refresh, any ideas?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    List View losing images on refresh, any ideas?

    #MODS PLEASE DISREGARD MY LAST TWO ATTEMPTS TO POST THIS, THIS IS THE ONE I WANT#

    Hi all,

    I'm hoping someone far smarter than me can help me with a small issue I have with an application I am developing.

    My application uses a list view that populates with images from files. The items in the list can be edited by the user including updating the menu images but when that happens the list view loses all it's images and won't reload them without re-booting the application.

    Here's a screen shot of my list view when you first start up the app

    Screeshot1

    ..which is correct. and here is the code that loads the images (lvBinSelect is the list view in question)..

    Code:
          Function UpdateCalcPicList()
    
            lvBinSelect.Items.Clear()
            ImageList1.Images.Clear()
            GC.Collect()
            MenuCntr = 0
    
            MenuTEXT = File.ReadAllLines(Application.StartupPath & "\App Files\Bins\Config.cfg")
    
            For Each i In MenuTEXT
                ImageList1.Images.Add(i, Image.FromFile(Application.StartupPath & "\App Files\Bins\" & i & "\Menu.png"))
                lvBinSelect.Items.Add(i, i)
            Next i
    
            Return True
        End Function
    I run this in the form load event and all is hunky dory. FYI, the Config.cfg file it is reading contains the name of the items which corresponds to the folder name their image is in.

    But now the issue of a user trying to change the image associated with each item. This is done on a different tab (there is a tab control being used I have just hidden the tabs with a panel with images to select instead and have moved the panel a bit so you can see what I mean in this next screenie)...

    Screenshot2

    When the button highlighted is pressed a dialogue appears allowing the user to pick a new png for the 'menu image' to be used in the list view on the previous tab. The code is as follows..

    Code:
            Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog
    
            openFileDialog1.Filter = "PNG Files (*.png)|*.png"
            openFileDialog1.FilterIndex = 1
            openFileDialog1.Multiselect = False
            openFileDialog1.Title = "Please select Menu PNG file"
    
    
            With openFileDialog1
                '...
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    'Open the selected file to read.
                    picBinSave.BackgroundImage.Dispose()
                    picMenuSave.BackgroundImage.Dispose()
                    GC.Collect()
                    ImageList1.Dispose()
                    lvBinSelect.Clear()
                    GC.Collect()
    
                    If File.Exists(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png") = True Then
                        FileIO.FileSystem.DeleteFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    End If
    
                    FileIO.FileSystem.CopyFile(openFileDialog1.FileName, Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
    
                    picBinSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Bin.png")
                    picMenuSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    
    Call UpdateCalcPicList()
    
    
                End If
    
            End With
    picBinSave and picMenuSave are the image boxes on the second tab you see in the last screen shot that previews the images. These refresh perfectly on updating.

    Now as you can see it calls the UpdateCalcPicList function again after clearing the list view which should in theory repopulate the list view with the updated images.

    But All I get is this after a user has changed an image and they go back to the first tab....

    Screenshot3

    The images are loading back into teh list view. The only way to get them back is to restart the application and it's fine again and load with the new image the user changed to. I have had to use 'Application.Restart()' after the change is made as a workwround but this is obviously not ideal.

    Can anyone help, much appreciation in advance.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: List View losing images on refresh, any ideas?

    Welcome to VBForums
    Quote Originally Posted by estebanrey View Post
    #MODS PLEASE DISREGARD MY LAST TWO ATTEMPTS TO POST THIS, THIS IS THE ONE I WANT#
    Thanks, that saved me some time.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Re: List View losing images on refresh, any ideas?

    No ideas anyone ?

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: List View losing images on refresh, any ideas?

    So, what's going on here then...

    Code:
    GC.Collect()   'There is no need to call this, it is done automatically when it needs to
     ImageList1.Dispose()    'You are disposing of the imagelist, why? How can you then expect it to be available later on unless you re-create it (which you don't), you also clear it in UpdateCalcPicList    so do need to do this
    lvBinSelect.Clear()  'Surely you want to items.clear, anyway again you do this in UpdateCalcPicList    
     GC.Collect()     'Again, not needed
    ..
    So your code should look like:

    Code:
    If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    'Open the selected file to read.
                    picBinSave.BackgroundImage.Dispose()
                    picMenuSave.BackgroundImage.Dispose()
    
                    If File.Exists(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png") = True Then
                        FileIO.FileSystem.DeleteFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    End If
    
                    FileIO.FileSystem.CopyFile(openFileDialog1.FileName, Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
    
                    picBinSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Bin.png")
                    picMenuSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    
                    UpdateCalcPicList()
                End If

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    5

    Re: List View losing images on refresh, any ideas?

    Quote Originally Posted by Grimfort View Post
    So, what's going on here then...

    Code:
    GC.Collect()   'There is no need to call this, it is done automatically when it needs to
     ImageList1.Dispose()    'You are disposing of the imagelist, why? How can you then expect it to be available later on unless you re-create it (which you don't), you also clear it in UpdateCalcPicList    so do need to do this
    lvBinSelect.Clear()  'Surely you want to items.clear, anyway again you do this in UpdateCalcPicList    
     GC.Collect()     'Again, not needed
    ..
    So your code should look like:

    Code:
    If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    'Open the selected file to read.
                    picBinSave.BackgroundImage.Dispose()
                    picMenuSave.BackgroundImage.Dispose()
    
                    If File.Exists(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png") = True Then
                        FileIO.FileSystem.DeleteFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    End If
    
                    FileIO.FileSystem.CopyFile(openFileDialog1.FileName, Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
    
                    picBinSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Bin.png")
                    picMenuSave.BackgroundImage = Image.FromFile(Application.StartupPath & "\App Files\Bins\" & lstBinList.SelectedItem & "\Menu.png")
                    
                    UpdateCalcPicList()
                End If
    Brilliant thanks, doh!

    The reason that code was there was because earlier I kept having a problem with trying to delete an image file that had been used by the software, that's when I found the GC.Collect thing which is used to clear the memory and it solved by issue at the time but I'd over-compensated with it and it didn't need to be where you said. the menu now works fine after adding a new item.

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