Member Avatar for DyO1

How can I make a Save/Open buttons that will save and open ListView items AND THEIR DETAILS? (tried anything and it won't save anything else but the name)

Each item in the ListView.Items collection has a SubItems collection. The data associated with the Item itself, is actually in SubItem(0). Therefore by iterating through all the SubItems from 0 to SubItems.Count - 1 you can save all the data from the Item.

Alternatively if you use a DataTable to hold the data and a DataGridView to display it, instead of a ListView. The DataTable Class has LoadXml and WriteXml methods, that will load/save the data with alot greater ease.

Member Avatar for DyO1

I kinda have trouble understanding you :-(
If you can please give me a code as example?

What in particular didn't you understand? I discussed 2 different concepts.

What code have you tried? It's a lot easier for you to understand if I show you, your code, modified, than if I just give you generic code.

Member Avatar for DyO1

Yeah I always submited the code "back in the days", sorry for not doing it again, kinda stopped coming here...so here's the code I use, it saves a listview items that have the details (Genre/Country/etc.), only names are saved (psst,that's bad) in a custom .RadioNET file

The code itself:

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

When you say, 'it saves a listview items' do you mean that you want to load a listview with data from a file? or do you want to save the listview data to a file?

When data files are involved it helps to have a sample of the data exactly how it appears in the file.

The code you've shown appears to try to load a listview with data. However it's only loading 1 column.

Member Avatar for DyO1

I want to "Export" (save listview items and their details) to my custom .RadioNEt file and then Import them (load the listview items and their details to a listview)

In other words, I want to save a file containing the listview content and details to a file.

Member Avatar for DyO1

I accidently gave you the Import code, here's the Export one:

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

Sorry, mate :-/

I assume you want to use something to delimit each piece of data, a comma perhaps?

Here's a way that will save all the data from each item on a line with commas separating the data:

    SaveFileDialog1.ShowDialog()
    Dim Path As String = SaveFileDialog1.FileName
    Try
        Dim sw As New IO.StreamWriter(Path)
        For Each item As ListViewItem In ListView1.Items
            sw.WriteLine(Join((From o In item.SubItems
                         Select DirectCast(o, ListViewItem.ListViewSubItem).Text).ToArray, ","c))
        Next
        sw.Close()
    Catch ex As Exception
    MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error")
    End Try

The main part of this code:

    sw.WriteLine(Join((From o In item.SubItems
                 Select DirectCast(o, ListViewItem.ListViewSubItem).Text).ToArray, ","c))

takes each subitem extracts the text value from it, puts the results into an array, then joins them all together in a string with a comma between each subitem, then writes the string to a line in the file.

You'll notice one of the things I took out was the check to delete the file if it's already present. The New IO.StreamWriter(Path) takes care of all that automatically.

Member Avatar for DyO1

Error 1 Expression of type 'System.Windows.Forms.ListViewItem.ListViewSubItemCollection' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.
Trying to figure it out what it means, but no luck :-/ Can you help me?

Try adding Imports System.LINQ to the top of the file.

Just in case your version of Visual Studio is too old to have LINQ, older than Framework 3.5, here is the long version without LINQ that does the same thing:

    Dim sw As New IO.StreamWriter("textfile2.csv")
    For Each item As ListViewItem In ListView1.Items
        Dim Line As New List(Of String)
        For Each si As ListViewItem.ListViewSubItem In item.SubItems
            Line.Add(si.Text)
        Next
        sw.WriteLine(Join(Line.ToArray, ","c))
    Next
    sw.Close()
commented: Thanks, worked ! +1
Member Avatar for DyO1

(I'm using VB.NET 2010 with .NET Framework disabled for the program, probably that's why it won't work)

Used the long SQL version,it worked well ( it saved the text with the details) but now I have and Importing problem, the details were showing on the first colum only with commas between them.

Tried to "mod" it by changing the 'write' text to 'read', but once I finished I got this error (probably because I can't do it right, have no idea what I'm doing >_<') *"Overload resolution failed because no accessible 'ReadToEnd' accepts this number of arguments."*

Same happens using "Read" and "ReadLine", this is the line with the error:

                sw.ReadLine(Join(Line.ToArray, ","c))

And this is the code I used before:

        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
Member Avatar for DyO1

PS: Long SQL version...wait what?

There's 2 operations you have to reverse when loading the data. Not only do you have to change the Write to Read, you have to change the Join to Split. Plus adding items is much simpler :

     Try
        ListView1.Items.Clear()
        Dim sr As New IO.StreamReader(Path)
        While Not sr.EndOfStream 
            ListView1.Items.Add(New ListViewItem(sr.ReadLine.Split({","c})))
        End While
    Catch ex As Exception
        MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error")
    End Try
commented: Thanks mate, everything is running good :-D best user I've found on this site yet :-) +0
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.