I have a TextFile that contains Records. On Form Load I m saving those records into
the listview. I set the MultiSelect property of the listview to true.
I have one button, I want that suppose user Selects multiple items
in the listview by using ctrl. & Click on Button,Firts records are deleted from the ListView
& Then From The Textfile.
Mine Code is as Under.

Plz tell me how to delete records from the TextFile.

Mine TextFile Records-
1^^Neha
1^^Mohit
2^^Ritu
3^^Ruchi
4^^Sneha

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
        Dim i As Integer
        Dim sFilename As String
        Dim sFileReader As System.IO.StreamReader
        Dim sarr() As String
        Dim sInputLine As String
        Dim lcount As Integer

 sFilename = "C:\Records.txt"
        If Dir(sFilename.ToString) <> "" Then
            sFileReader = System.IO.File.OpenText(sFilename)
            sInputLine = sFileReader.ReadLine()
            Do Until sInputLine Is Nothing
                sarr = Split(sInputLine, "^^")
                Call SaveToListView2(sarr(0), sarr(1))
                sInputLine = sFileReader.ReadLine()
            Loop
        End If
    End Sub

 Private Sub SaveToListView2(ByVal roll As String, ByVal name As String)
        lvitem = ListView2.Items.Add(roll)
        lvitem.SubItems.Add(name)
    End Sub

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RowNum As ListViewItem

        For Each RowNum In ListView2.SelectedItems
            ListView2.Items.Remove(RowNum)
        Next

    End Sub

Button1_Click Code Deletes the Row well if we choose the row that has roll 2,3 ,4.
Suppose If I want to delete the Record 1^^Neha.
Then the Error is coming??
Plz tell me dat.
Second tell me how to delete that record from the TextFile Cz I have no idea how to do it??

Suppose If I want to delete the Record 1^^Neha.
Then the Error is coming??

Hi, this mean you want to raise error, while delete this record?.

how to delete that record from the TextFile Cz I have no idea how to do it??

One idea
You are loading the records from File to Listview. For deleting do the reverse, that is clear the file (or open file in write mode not append mode this will clear the content of a file) and save the all records currently in ListView.

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.