Jeff Ledwin Jeff Ledwin - 5 months ago 26
Vb.net Question

Separate String Lines using StreamReader | VB.net

I am trying to make my own checklist app. I am using a ListView object to display each checklist item. I can add individual items to the object. I don't know how to save the ListView items on exit and then load them in on startup. (I tried using My.Settings, but it doesn't work.)

My solution was to make an import/export system using .txt files to store data. They are formatted like this:

Item1

Item2

Item3

When I import them, it all shows as one long item in ListView. I am using the code below.

Private Sub ChooseFileButton_Click(sender As Object, e As EventArgs) Handles ChooseFileButton.Click
If ImportFileDialog.ShowDialog = DialogResult.OK Then
Dim fileReader As String
fileReader =
My.Computer.FileSystem.ReadAllText(ImportFileDialog.FileName)
ImportFileDialog.RestoreDirectory = True

ChecklistObject.Items.Add(fileReader)
End If
End Sub


If anyone knows how to write individual items on their own line in a text file, that would be great too.

EDIT: Exporting doesn't work either. Using code below:

Private Sub ExportButton_Click(sender As Object, e As EventArgs) Handles ExportButton.Click
ExportFileDialog.Filter = "Keklist Save|*.kek"
If ExportFileDialog.ShowDialog = DialogResult.OK _
Then
ChecklistObject.Items.Item()
End If
End Sub

Answer

In your example code you are only adding one item to the list view so it will only show the one line.

You could use System.IO.File.ReadAllLines to read all the lines of the file into a string array.

Private Sub ChooseFileButton_Click(sender As Object, e As EventArgs) Handles ChooseFileButton.Click
    If ImportFileDialog.ShowDialog = DialogResult.OK Then
        Dim path As String = ImportFileDialog.FileName
        Dim lines() As String = File.ReadAllLines(path)
        ImportFileDialog.RestoreDirectory = True
        For Each line in lines
            ChecklistObject.Items.Add(line)
        Next
    End If
End Sub
Comments