Click here to Skip to main content
15,886,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried before to post my question with the code I have made in order to get some feed back as to where I have gone wrong but maybe its all wrong so......I will simply ask a question in hopes someone will post a working version of a checklistbox delete method that removes the item withing the checklistbox along with the actual file on the computer. I would also like to know if there is more than one method to delete files from your computer by using a checklistbox?
Posted

VB
Dim filelist As New List(Of String)
    Private Sub AddFile(ByVal Item As String)
        Dim F As New FileInfo(Item)
        CheckedListBox1.Items.Add(F.Name)
        filelist.Add(F.FullName)
    End Sub

    Private Sub RemoveFile(ByVal Index As Integer)
        File.Delete(filelist.Item(Index))
        CheckedListBox1.Items.RemoveAt(Index)
        filelist.RemoveAt(Index)
    End Sub

    Private Sub FillList(ByVal Path As String)
        Me.CheckedListBox1.Items.Clear()
        filelist = New List(Of String)
        For Each filepath As String In Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories)
            AddFile(filepath)
        Next
    End Sub

    'Change all instances of CheckedListBox1 to your list box

    'To get all the files you need use FillList with the path as your directory tog et files from
    'This should populate the checkboxlist and it's sister list

    'The sister list contains the full file path where as the checkbox list only contains the name of the file

    'To remove checked files:

    Private Sub DeleteSelectedIndices()
        For Each i As Integer In CheckedListBox1.SelectedIndices
            RemoveFile(i)
        Next
    End Sub


Be sure to Import File.IO

Imports File.IO
(above the class)
 
Share this answer
 
Comments
Dale 2012 9-Nov-10 23:11pm    
This is a real nice example Im sure but I need it to be in the form of a click button event or can I add this code to my existing code?
Dale 2012 9-Nov-10 23:12pm    
Im not sure you can be more specific but if you can walk me through where the different parts of code are applied to that would be awesome.
The ANZAC 9-Nov-10 23:15pm    
You should be able to copy all this into your class. Then as I have instructed, change all instances of ChekedListBox1 to whatever your listbox is called. Then add a button onto your form that is delete selected and in that add:

DeleteSelectedIndices()
Dale 2012 9-Nov-10 23:30pm    
The program I am working on is a virus scanner so I need it to find and remember the path when a file is found so that I can delete it when the name of that file is shown in the checklistbox.
Dale 2012 9-Nov-10 23:31pm    
So I should be able to just copy and pase it all where ever into my project?
Well I'm assuming you are reading files and adding their name to a checkbox or something like that?

If this is the case, then for each item you add, you also need to keep track of the full path of the file it is associated with.

Then when a checkbox is checked and chosen to be deleted, you retrieve the full path of the selected item and use:

File.delete(#path#)

Where #path# is the full path name of the file.
 
Share this answer
 
v2
Comments
Dale 2012 8-Nov-10 23:27pm    
where and how can I take note of the full path once an item has been added into the checklistbox? If you are willing to give a code example it will be saving my program 100% because right now I have been stuck on this problem for weeks
The ANZAC 8-Nov-10 23:29pm    
Well how are you finding the files?
Dale 2012 9-Nov-10 21:06pm    
I am using a text file loaded into my resources that lists names to look for in my computer.
Dale 2012 9-Nov-10 23:28pm    
what if the path of the file is unknown untill my scan finds it or how can I use my scan to fill in the blanks so to speak?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900