Question ListView multicolumn export and import items

wojtek1150

Member
Joined
Sep 14, 2011
Messages
5
Programming Experience
1-3
Hello,

I got one problem. I did simple listview-saver, (with 1 line) and all working (import, export)...

But i want to add new colums. But I don't know how to export or inport them.

Code:
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox4.Text = TextBox1.Text & vbNewLine & TextBox2.Text & vbNewLine & TextBox3.Text & vbNewLine
        Dim str(3) As String
        Dim itm As ListViewItem
        str(0) = TextBox1.Text
        str(1) = TextBox2.Text
        str(2) = TextBox3.Text
        itm = New ListViewItem(str)
        ListView1.Items.Add(itm)
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub


    Private Sub ZamknijToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZamknijToolStripMenuItem1.Click
        Me.Close()
    End Sub


    Private Sub NowyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NowyToolStripMenuItem.Click
        ListView1.Items.Clear()
    End Sub


    Private Sub ZapiszToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZapiszToolStripMenuItem.Click
        SaveFileDialog1.ShowDialog()
        Dim Path As String = SaveFileDialog1.FileName
        Dim AllItems As String = ""
        Try
            For itm = 0 To ListView1.Items.Count - 1
                AllItems = AllItems & ListView1.Items.Item(itm).Text & vbNewLine
            Next
            AllItems = AllItems.Trim
        Catch ex As Exception
        End Try
        Try
            If My.Computer.FileSystem.FileExists(Path) Then
                My.Computer.FileSystem.DeleteFile(Path)
            End If
            My.Computer.FileSystem.WriteAllText(Path, AllItems, False)
        Catch ex As Exception
            MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try
    End Sub


    Private Sub OtwórzToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OtwórzToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        Dim Path As String = OpenFileDialog1.FileName
        Dim AllItems As String


        Try
            AllItems = My.Computer.FileSystem.ReadAllText(Path)


            Dim ItemLines As New TextBox
            ItemLines.Text = AllItems


            For Each line As String In ItemLines.Lines
                ListView1.Items.Add(line)


            Next
        Catch ex As Exception
            MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try
    End Sub
End Class




This code is with adding items to colums, but it saving only first item from line. Anybody know how to export and import all items from line?

Please help
 
Last edited:
Use a TextFieldParser to read the data, line by line and field by field. The documentation has an example. The documentation also has an example of loading data into a ListView with multiple columns. As for saving, you can use nested loops, either For or For Each. Loop through the Items in the ListView and then the SubItems of each item.
 
Back
Top