hey guys
can u help me with this the problem of this code should delete only the selected item from the listview in a report.txt but this code delete all the data in the txt file whether i selected only one data from the listview....

Dim myFile As String = "C:\report.txt"
        Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)
        Dim writer As New IO.StreamWriter(stream)
 
         For Each item As ListViewItem In ListView1.SelectedItems
                    item.Remove()
                    MsgBox("File Deleted Permanently.", MsgBoxStyle.Information)
                Next
        Next
        writer.Flush()
        writer.Close()
        stream.Close()

Recommended Answers

All 2 Replies

This will remove the item from the ListView however I am puzzled how the listview relates to the report.txt file as there is no interaction with it in the code you posted

Dim myFile As String = "C:\report.txt"
 
      Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)

      Dim writer As New IO.StreamWriter(stream)
      
     ListView1.Items.RemoveAt(ListView1.SelectedIndex)
 
      MsgBox("File Deleted Permanently.", MsgBoxStyle.Information)
  
 

      writer.Flush()

      writer.Close()

      stream.Close()

that code i just modify from my code that save the data in txt file..this is my code for saving listview data in txt file..

Dim myFile As String = "C:\report.txt"
      Dim stream As New IO.FileStream(myFile, IO.FileMode.Create)
      Dim writer As New IO.StreamWriter(stream)
       
      writer.WriteLine("First name,lastname,address")
       
      For Each item As ListViewItem In ListView1.Items
      writer.WriteLine(item.SubItems(0).Text & "," & item.SubItems(1).Text & ","
      & item.SubItems(2).Text)
      Next
      writer.Flush()
      writer.Close()
      stream.Close()

so i try that when i click it should remove from the txt file the data that is selected in my listview. only the selected data should be remove and that my item.remove() it remove all the data in the txt file even if i did not click them

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.