Search code examples
vb.netlistviewdisposeimagelist

unlocking and deleting files from listview


I got a difficult question for you guys...

Ive been populating a listview item with this code:

    Dim loc5 As String
    loc5 = "C:\Users\JoseLuis\Dropbox\files\0. Image\4. Galería\Collection"

    Dim il As New ImageList
    il.ColorDepth = ColorDepth.Depth32Bit
    ListView1.LargeImageList = il
    ListView1.SmallImageList = il
    ListView1.LargeImageList.ImageSize = New System.Drawing.Size(28, 28)


    For Each file In System.IO.Directory.GetFiles(loc5)
        Dim list As New ListViewItem(IO.Path.GetFileName(file.ToString))
        Dim listnoex As New ListViewItem(IO.Path.GetFileNameWithoutExtension(file.ToString))
        list.Text = IO.Path.GetFileName(file.ToString)
        listnoex.Text = IO.Path.GetFileNameWithoutExtension(file.ToString)
        If list.Text.Contains(".PNG") Or list.Text.Contains(".png") Then
            il.Images.Add(file, Drawing.Image.FromFile(file))
            ListView1.Items.Add(listnoex.Text, il.Images.Count - 1)
        End If
    Next

It gets the thumnbnail from a bunch of pngs from a file...

Everything is working great until i get to the delete button...

    Dim loc5 As String
    Dim named As String

    loc5 = "C:\Users\JoseLuis\Dropbox\files\0. Image\4. Galería\Collection\"

    Dim item As ListViewItem
    item = ListView1.SelectedItems.Item(0)
    named = loc5 & (item.Text) & ".png"
    item.Remove()
    My.Computer.FileSystem.DeleteFile(Name, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)

Everytime I try to delete the selected file it seems to be still in use by the button that populated the list...

Any clues on how to dispose the selected item and image from the listview?

Thanks in advance


Solution

  • Thanks everybody for your reply. Visual Vincent is right, I meant named lol.

    Solved! , one nifty little line solved my problem: GC.Collect() after the item removal from the listview.

    Thanks again for your expertise. 😊