Hello everyone, Im a beginner in vb.net and currently Im developing a program where i save date to a txt file like this Reminder#dinner#26-07-2012 19:52:00 and show it in a listbox(all the lines in the txt file).
What i want to do is to select a line(item) in the listbox and delete it in the txt file.

How can i delete a line of a txt file by selecting it from the listbox? I already can check if the line(i) its equal to the selected item of the textbox

Heres the code that i have:

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, task_viewer.Click

        Dim lines() As String = IO.File.ReadAllLines("C:\Users\Guilherme\Documents\database\data_base_tasks.txt")
        For i As Integer = 0 To UBound(lines)
            If lines(i) = task_viewer.SelectedItem.ToString Then
                MsgBox(lines(i)) ' this was to see if the selected item in the listbox would show the line that was on the txt file, and it shows
            End If

        Next
    End Sub

Recommended Answers

All 12 Replies

Listbox.items.removeat(Passtheindex) And writeback all the remaining lines to textfile again..

Wasn't this already asked and answered here?

commented: Good catch, Iggy. +5

Here I think this is what you want.

Public Class MyForm
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, task_viewer.Click
        Dim lines() As String = IO.File.ReadAllLines("C:\Users\Guilherme\Documents\database\data_base_tasks.txt")
        MyListBox.Items.Clear()
        For Each Line As String In lines
            MyListBox.Items.Add(Line)
        Next
    End Sub

    Private Sub RemoveLineButton_Click(sender As Object, e As EventArgs) Handles RemoveLineButton.Click
        If MyListBox.SelectedItems.Count > 0 Then
            MyListBox.Items.RemoveAt(MyListBox.SelectedIndex)
            Dim MyString As String
            For Count As Integer = 0 To MyListBox.Items.Count - 1
                MyString &= MyListBox.Items.Item(Count) & Environment.NewLine
            Next
            My.Computer.FileSystem.WriteAllText("C:\Users\Guilherme\Documents\database\data_base_tasks.txt", MyString, False)
        End If
    End Sub
End Class

Checked & Works!

Well, not exactly, because in that post "replace a line of a txt file" I said that I needed to change somethings in the line but in this post I need to delete the line from the txt file. i can put lines(i)="", but by putting like this I get a line without nothing in the TXT file and the contents of it are going to be displayed in a listbox. I dont want to display a line without nothing. I searched on google but there things that I dont understand. i tried to do what Pgmer told me but I couldnt put it to work and I tried this but it doesnt work too.

        Dim lines() As String = IO.File.ReadAllLines("C:\Users\Guilherme\Documents\database\data_base_tasks.txt")
        For i As Integer = 0 To UBound(lines)
            If lines(i).Contains(task_viewer.SelectedItem.ToString) Then
               ' MsgBox(lines(i))
                lines(i).Remove(lines(i))
            End If

        Next

my last post was to awnser Reverend jim, i didint saw that you had posted pride

Hello Pride that code that you made works, but i dont understand somethings in the code, could you explain it to me?

   If MyListBox.SelectedItems.Count > 0 Then 'Here when i click in a item of the listbox the count goes to 1 right?
            MyListBox.Items.RemoveAt(MyListBox.SelectedIndex)
            Dim MyString As String
            For Count As Integer = 0 To MyListBox.Items.Count - 1 'I dont understand this part of here
                MyString &= MyListBox.Items.Item(Count) & Environment.NewLine 'And this part of code
            Next
            My.Computer.FileSystem.WriteAllText("C:\Users\Guilherme\Documents\database\data_base_tasks.txt", MyString, False) ' this false here i saw it on microsoft and it says it is for the append of the text but by putting false, you overwrite the text? is that it?
        End If

If lines() is the original array of lines from the text file (that you have copied to the textbox for display), you can remove a particular line (or lines) from the array by using the Filter function

dim newlines() as String = Filter(lines,"some string",include)

If include=True then Filtere will return all of the lines that contain the string. If include=False then all lines NOT containing the string will be returned. The easiest way to maintain the list is to apply filter every time an item is removed from the listbox as in

lines = Filter(lines,removedText,False)

guilherme.carvalho.9250, I put the false there since its not saving it line by line, but rather the whole thing at a time. If it were saving it line by line, then I would have made it true. But depends on you, if you have data in your text file, and want to add more, then just make that true, otherwise false.

@Pride

What I said has nothing to do with saving. It doesn't matter if lines() is saved en masse to the file or a line at a time. All that Filter does is return an array of strings that either contain or do not contain the given string. If the intention was to read or save the lines one at a time then the array would not be necessary. The lines could be read into or save directly from the ListBox control.

Hello to everyone, thank you Reverend Jim, Pqmer and Pride for helping me solve this problem! I understood your code Pride, and what you Reverend Jim said about the filter function I googled it and now I understand it better. Its doing what i want now. Thank you all.

I have one more question, just by writting task_viewer.selecteditem and not with the (.tostring) it does the same thing, so task_viewer.selecteditem(only) returns a string?

Yes indirectly, they're the same same.
Without the .ToString, it returns a value as an object.
With the .ToString, it clearly returns a string value.

If that's what your asking.

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.