Hi, Daniweb members
i have an listview containing an normal listviewitem plus one subitem
What i want to do is save the info in the listview in a textfile and load when i start my program. So that all the info will be saved, but how do i do this?

Hope somebody can help me, Killerbeat

See if this helps.
Prerequisites: 1 ListView, 1 Button.

Public Class Form1

    Private myCoolFile As String = "C:\test.txt" '// your file.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim myWriter As New IO.StreamWriter(myCoolFile)
        For Each myItem As ListViewItem In ListView1.Items
            myWriter.WriteLine(myItem.Text & "#" & myItem.SubItems(1).Text) '// write Item and SubItem.
        Next
        myWriter.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.View = View.Details : ListView1.Columns.Add("column 1") : ListView1.Columns.Add("column 2")

        If IO.File.Exists(myCoolFile) Then '// check if file exists.
            Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your file as a string array.
            For Each line As String In myCoolFileLines '// loop thru array list.
                Dim lineArray() As String = line.Split("#") '// separate by "#" character.
                Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
                newItem.SubItems.Add(lineArray(1)) '// add SubItem.
                ListView1.Items.Add(newItem) '// add Item to ListView.
            Next
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static i As Integer = 0
        Dim newItem As New ListViewItem("item " & i) '// add text Item.
        newItem.SubItems.Add("subitem " & i) '// add SubItem.
        ListView1.Items.Add(newItem) '// add Item to ListView.
        i += 1
    End Sub
End Class
commented: Great Snipped, thanks +1

Thanks codeorder, it worked perfectly :D

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.