Member Avatar for DyO1

I would like to make a Export / Import buttons that will save/open files that contain listView data (ListView Items)

Can someone tell me is there a way to do this?

Recommended Answers

All 6 Replies

Yes, of course there's a way to do that. It's pretty straightforward in that you open the file and then read or write the data. However, it does depend somewhat on the format you want for the file.

Member Avatar for DyO1

Can I use a custom made format, or I need to use something like .xml

You can use whatever format you want.

Member Avatar for DyO1

Isn't the write and read stuff for text only?
just curius how it's going to work out, or I have to use a custom/special code so it saves it as list with details and not like text (that would be weird...)

To write the file you'll loop through the items and save them to a text file (or a binary file if you so choose). There's a conversion involved to produce the file format you want.

To read the file you'll parse it, convert each record back to something that can be applied to a ListView item, apply it, and repeat for each record until the file is exhausted.

I get the impression that you think there will be a one line solution that magically converts a file to a ListView, which itself would require a bit of scaffolding that you'd have to write as well, if you want it.

Member Avatar for DyO1

Not that I understood you clearly, but I made it somehow... :D

    Private Sub ExportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToolStripMenuItem.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 ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.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
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.