I am making a program to list all files in a listview (currently it lists the file name, file path, last access date, and size) in a specified directory, and I want the user to be able to select files using checkboxes, click a delete button, and see those files listed the same way on another form in a listbox. I've been able to get the first column of data (the file names) to be passed over to the second form but can't get the rest of the info to show up. The user should then be able to delete the files from here. I hope this makes sense...the code i'm using to pass the checked items is below. Any tips?

Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
        For Each unwantedFile As ListViewItem In FilesList.Items
            If unwantedFile.Text Is Nothing Then
                MessageBox.Show("You have not checked any items to be deleted", "Error")
            Else
                If unwantedFile.Checked = True Then
                    DeleteForm.DeletionList.Items.Add(unwantedFile.Text)
                End If
            End If
        Next
DeleteForm.Show()
End Sub

Then the code I have to delete the files from the second form is below - somehow the types don't match up:

Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
        For Each file As ListViewItem In DeletionList.Items
            If file.Checked = True Then
                For Each item As String In DeletionList.Items
                    IO.File.Delete(item)
                Next
            End If
        Next
    End Sub

I'm somewhat new to .net so any advice or pointers would be lovely. Thanks so much!

~Sara

Ranjith.M commented: ya, is ok ..but i have best coding for move to listview1 to listview2 +0

Hi sdimantova,

In your code try changing the following:

If unwantedFile.Checked = True Then
                    DeleteForm.DeletionList.Items.Add(unwantedFile.Text)
      End If

To this:

If unwantedFile.Checked = True Then
                    DeleteForm.DeletionList.Items.Add(unwantedFile.Clone)
      End If

The problem is your only adding the Text of each listviewitem in the first column, what you want to do however is clone the entire listview item!

Hope this helps you out.

That worked wonderfully, thank you!
Now what I need to do is get the files that are checked to either be deleted or enable the user to move them to another specified directory. I am getting the same error for both options - again an issue with the types not matching up. What I have so far is this:

Private Sub MoveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveBtn.Click
For Each file As ListViewItem In DeletionList.Items
            If file.Checked = True Then
                For Each file2 As String In DeletionList.Items
                    IO.File.Move(file2, MoveTxtBox.Text)
                Next
            End If
        Next
    End Sub

And for the delete function:

For Each file As ListViewItem In DeletionList.Items
            If file.Checked = True Then
                For Each item As String In DeletionList.Items
                    IO.File.Delete(item)
                Next
            End If
        Next

I'm assuming whatever fixes one function will fix the other...but I have no idea where to go from here. Any ideas? Thanks!

Here is the code you have (slightly amended), though you don't actualy need the second For Each statement in there! Just remember, in the MoveTxtBox, you must also supply the file name and extension. E.g: C:\Users\UserName\Desktop\NewFileName.txt!

For Each file As ListViewItem In deletionList.Items
            If file.Checked = True Then
                     IO.File.Move(file.Text, MoveTxtBox.Text)
              '  For Each file2 As ListViewItem In deletionList.Items
                 '   IO.File.Move(file2.Text, MoveTxtBox.Text)
             '   Next
            End If
        Next

How can I get it to append the file path of the original file to the actual file name so it knows which file it's supposed to be moving - I would have to get the info in the first column and in the second column where the file path is stored, but I'm not sure how to do this. Sorry for so many questions...I really appreciate the help!!

No problem, glad to help! In your For Each loop try doing this:

Dim filePath As String = file.SubItems(1).Text & "\" & file.Text

You can use the SubItems Property along with a specified Column index () to retrieve the path you need! You can now use the filePath variable instead of the file.Text property.

Hope this helps :D

Perfect. Thank you!

Your more than welcome, glad you solved it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.