22 | 02 | 2017

Listview to String and String to Listview

The function below are able to store listview content into a string. With the function ImportListViewfromString the string can be used tot populate a listview again.

 

 

 Function ImportListViewfromString(ByVal ListViewString As String, ByVal LSTview As ListView) As Boolean
        On Error GoTo errorhandler
        ImportListViewfromString = False
        'This function populates a listview with content from a string

        Dim ItemLines As New TextBox
        ItemLines.Text = ListViewString

        For Each line As String In ItemLines.Lines
            Dim a1() As String = line.Split("|") 'Change | if a different delimiter is used
            Dim Item As New ListViewItem(a1(0))
            For i = 1 To UBound(a1)
                Item.SubItems.Add(a1(i))
            Next i
            LSTview.Items.AddRange(New ListViewItem() {Item})
        Next

        ImportListViewfromString = True
        Exit Function
errorhandler:
        MsgBox(Err.Description)
    End Function

    Function ExportListViewtoString(ByVal LSTview As ListView) As String
        'This function populates a string with content from a listview
        On Error GoTo errorhandler

        Dim String1 As String = ""
        For Each ListViewitem1 As ListViewItem In LSTview.Items
            If String1 <> "" Then String1 = String1 & vbCrLf
            String1 = String1 & ListViewitem1.Text & "|" 'Change | if a different delimiter is used

            For i = 1 To ListViewitem1.SubItems.Count - 1
                String1 = String1 & ListViewitem1.SubItems(i).Text & "|"
            Next

        Next
        ExportListViewtoString = (String1)
        Exit Function
errorhandler:
        MsgBox(Err.Description)
    End Function
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2174937
Latest Articles