none
Save User Data on ListView

    Question

  • I have a problem with saving userdata, my main problem is that I allready have saved user data on listbox, now for each of selected item how can I save listview for it.

    Imagine that I have frmListBox that after selecting item, on listview you add customers and I want to save the listview for each of selected item from listbox.

    Any suggestions ?

    Thursday, October 17, 2013 8:55 PM

Answers

  • Hi,

     You can use the System.Collections.Specialized.StringCollection kind of like you would a delimited text file. Add each columns text together with a delimiter character and then add it to the Specialized.StringCollection. Do the same for each row in the listview. Also you need to initialize the Specialized.StringCollection before you can use it or you will get an error. Here is an example of saving and loading the items from a listview that has 3 columns.

    EDIT: I forgot to mention that the LV_Items is the Specialized.StringCollection that i added to the applications settings.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'The following is just to set up the listview properties and add 3 columns.
            ListView1.View = View.Details
            ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
            ListView1.Columns.Add("Column1", 70)
            ListView1.Columns.Add("Column2", 70)
            ListView1.Columns.Add("Column3", 70)
    
            'This initializes the Specialized.StringCollection if it has not been initalized
            If My.Settings.LV_Items Is Nothing Then
                My.Settings.LV_Items = New Specialized.StringCollection
            End If
        End Sub
    
        Private Sub Button_LoadItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LoadItems.Click
            For Each lvrow As String In My.Settings.LV_Items
                Dim itms() As String = lvrow.Split(CChar("‡"))
                Dim lvi As New ListViewItem(itms(0))
                For i = 1 To itms.Length - 1
                    lvi.SubItems.Add(itms(i))
                Next
                ListView1.Items.Add(lvi)
            Next
        End Sub
    
        Private Sub Button_SaveItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SaveItems.Click
            If ListView1.Items.Count > 0 Then
                My.Settings.LV_Items.Clear()
                Dim itmstxt As String = ""
                For Each itm As ListViewItem In ListView1.Items
                    itmstxt = itm.Text
                    For i = 1 To ListView1.Columns.Count - 1
                        itmstxt &= "‡" & itm.SubItems(i).Text
                    Next
                    My.Settings.LV_Items.Add(itmstxt)
                Next
                My.Settings.Save()
            End If
        End Sub
    End Class



    • Edited by IronRazerz Friday, October 18, 2013 8:42 PM
    • Proposed as answer by Armin Zingler Friday, October 18, 2013 8:46 PM
    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Friday, October 18, 2013 8:36 PM
  • Try changing the For loop lines like this, adding the (As Integer) to them

    For i As Integer = 1 To ListView1.Columns.Count - 1

    and

    For i As Integer = 1 To itms.Length - 1

    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Friday, October 18, 2013 11:42 PM
  • I don't know why it isn't working, I've uploaded my project here:

    Proj2

    Can you take a look please

    After looking at your code i have spotted a few things you should fix. The first is at the top of the code where you declare the strFileName and assign the pathname to it. The \ character is missing between the directory and the filename. You should use Path.Combine to add the filename to the directory like this

        Dim strFileName As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "test-k.txt")
    

    The next thing is your frmKupci_Load event sub is missing (Handles MyBase.Load) at the end of it. It should look like this

    Private Sub frmKupci_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    I am taking it that Button1 is to add the 7 textboxes text to each column of the listview. It should be something like this

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim newItm As New ListViewItem(TextBox1.Text)
            newItm.SubItems.Add(TextBox2.Text)
            newItm.SubItems.Add(TextBox3.Text)
            newItm.SubItems.Add(TextBox4.Text)
            newItm.SubItems.Add(TextBox5.Text)
            newItm.SubItems.Add(TextBox6.Text)
            newItm.SubItems.Add(TextBox7.Text)
            List.Items.Add(newItm)
        End Sub
    

    After fixing that your code seemed to work fine. I did not run your project in VB, i just copied the code from Form1 and pasted it into a new project so i did not know if you have the ListView`s View property set to Details but, that needs to be set if you don`t already.

    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Saturday, October 19, 2013 12:48 AM

All replies

  • I have a problem with saving userdata, my main problem is that I allready have saved user data on listbox, now for each of selected item how can I save listview for it.

    Imagine that I have frmListBox that after selecting item, on listview you add customers and I want to save the listview for each of selected item from listbox.

    Any suggestions ?

    It's not clear (to me, at least) what you have and what you want.

    Are you using controls as a means of data storage? Can you explain more please?


    Please call me Frank :)

    Thursday, October 17, 2013 10:25 PM
  • Can you try to solve this for me, if It works as planed this topic will be anwsered :)

    I dont understand why I'm getting this. In form_load is this code

            If My.Settings.kpci.Count > 0 Then
                For Each item As String In My.Settings.kpci
                    List.Items.Add(item)
                Next
            End If

    on button close is this code

    Friday, October 18, 2013 12:41 PM
  • Can you try to solve this for me, if It works as planed this topic will be anwsered :)

    I dont understand why I'm getting this. In form_load is this code

            If My.Settings.kpci.Count > 0 Then
                For Each item As String In My.Settings.kpci
                    List.Items.Add(item)
                Next
            End If

    on button close is this code

    You can't store a collection (the ListView items) in a string any more than you could store them into an integer or boolean.

    Change the type (in the "My.Settings") to a collection type and you'll need to instantiate it or it will show another exception.


    Please call me Frank :)

    Friday, October 18, 2013 12:44 PM
  • The problem it's set to collection type
    Friday, October 18, 2013 5:21 PM
  • You are casting the items in the listview to String. But they are of type ListViewItem, so cast to ListViewItem. Then take each Item's Text property:

    From this In list.Items.Cast(Of ListViewItem)() Select this.Text

    EDIT: Or:

    From this In List.Items Select DirectCast(this, ListViewItem).Text


    Armin


    Friday, October 18, 2013 5:29 PM
  • @Armin Zingler

    I can't use that line, becouse Option Strict is On





    Friday, October 18, 2013 5:36 PM
  • I can't use that line, becouse Option Strict is On

    It works here. What is the exact type of My.Settings.kpci? What is your complete code line?

    Armin

    Friday, October 18, 2013 5:38 PM
  • Now you have

        My.Settings.kpci.AddRange(From this In List.Items Select DirectCast(this, ListViewItem).Text)

    You have deleted the .ToArray call. Add it back as it was before, and it will work.


    Armin

    Friday, October 18, 2013 5:45 PM
  • I still get an error :

    Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Object,System.String]' to type 'System.String[]'.

    Friday, October 18, 2013 5:47 PM
  • Please post the whole line you have now. Only the one line.

    Armin

    Friday, October 18, 2013 5:49 PM
  •     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            My.Settings.kpci.Clear()
            If List.Items.Count > 0 Then
                My.Settings.kpci.AddRange(From this In List.Items Select DirectCast(this, ListViewItem).Text)
            End If
            Me.Close()
        End Sub

    The button that exits the program, I've posted in few reply's before the whole code of a form

    Friday, October 18, 2013 5:50 PM
  • You haven't made the change I suggested before. Calling ToArray is still missing.

    Armin

    Friday, October 18, 2013 5:52 PM
  • Still getting error

    Friday, October 18, 2013 5:55 PM
  • Compare it to what you had before. The braces are missing.

    Once again from the start. Before, you had:

    My.Settings.kpci.AddRange((From this In List.Items.Cast(Of String)() Select this).ToArray)

    Then I told you the right expression is:

    From this In List.Items Select DirectCast(this, ListViewItem).Text

    So, if you just replace it, you have:

    My.Settings.kpci.AddRange((From this In List.Items Select DirectCast(this, ListViewItem).Text).ToArray)


    Armin

    Friday, October 18, 2013 6:00 PM
  • The problem is that this saves only first column not all data.
    Friday, October 18, 2013 6:33 PM
  • The problem is that this saves only first column not all data.
    I do not use My.Settings, so I don't know which other options you have. For sure someone else will.

    Armin

    Friday, October 18, 2013 6:39 PM
  • Hi,

     You can use the System.Collections.Specialized.StringCollection kind of like you would a delimited text file. Add each columns text together with a delimiter character and then add it to the Specialized.StringCollection. Do the same for each row in the listview. Also you need to initialize the Specialized.StringCollection before you can use it or you will get an error. Here is an example of saving and loading the items from a listview that has 3 columns.

    EDIT: I forgot to mention that the LV_Items is the Specialized.StringCollection that i added to the applications settings.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'The following is just to set up the listview properties and add 3 columns.
            ListView1.View = View.Details
            ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
            ListView1.Columns.Add("Column1", 70)
            ListView1.Columns.Add("Column2", 70)
            ListView1.Columns.Add("Column3", 70)
    
            'This initializes the Specialized.StringCollection if it has not been initalized
            If My.Settings.LV_Items Is Nothing Then
                My.Settings.LV_Items = New Specialized.StringCollection
            End If
        End Sub
    
        Private Sub Button_LoadItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LoadItems.Click
            For Each lvrow As String In My.Settings.LV_Items
                Dim itms() As String = lvrow.Split(CChar("‡"))
                Dim lvi As New ListViewItem(itms(0))
                For i = 1 To itms.Length - 1
                    lvi.SubItems.Add(itms(i))
                Next
                ListView1.Items.Add(lvi)
            Next
        End Sub
    
        Private Sub Button_SaveItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SaveItems.Click
            If ListView1.Items.Count > 0 Then
                My.Settings.LV_Items.Clear()
                Dim itmstxt As String = ""
                For Each itm As ListViewItem In ListView1.Items
                    itmstxt = itm.Text
                    For i = 1 To ListView1.Columns.Count - 1
                        itmstxt &= "‡" & itm.SubItems(i).Text
                    Next
                    My.Settings.LV_Items.Add(itmstxt)
                Next
                My.Settings.Save()
            End If
        End Sub
    End Class



    • Edited by IronRazerz Friday, October 18, 2013 8:42 PM
    • Proposed as answer by Armin Zingler Friday, October 18, 2013 8:46 PM
    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Friday, October 18, 2013 8:36 PM
  • Thank you IronRazerz, can you tell me on my form how can I save the data to .txt file and load them again?

    Friday, October 18, 2013 9:31 PM
  • Here is an example of using the same method but, in this it saves it and loads it to and from a text file. I set the FilePathName variable to use the ApplicationData folder to save the file in but, you can set the path to whatever you need it to be. The form is set up the same as the last example with 1 listview and 2 buttons on it.

    Public Class Form1
        Dim FilePathName As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ListViewItems.txt")
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'The following is just to set up the listview properties and add 3 columns.
            ListView1.View = View.Details
            ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
            ListView1.Columns.Add("Column1", 70)
            ListView1.Columns.Add("Column2", 70)
            ListView1.Columns.Add("Column3", 70)
        End Sub
    
        Private Sub Button_SaveItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_SaveItems.Click
            If ListView1.Items.Count > 0 Then
                Dim filelines As New List(Of String)
                Dim itmstxt As String = ""
                For Each itm As ListViewItem In ListView1.Items
                    itmstxt = itm.Text
                    For i = 1 To ListView1.Columns.Count - 1
                        itmstxt &= "‡" & itm.SubItems(i).Text
                    Next
                    filelines.Add(itmstxt)
                Next
                IO.File.WriteAllLines(FilePathName, filelines.ToArray)
            End If
        End Sub
    
        Private Sub Button_LoadItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LoadItems.Click
            If IO.File.Exists(FilePathName) Then
                ListView1.Items.Clear()
                Dim filelines() As String = IO.File.ReadAllLines(FilePathName)
                For Each fileline As String In filelines
                    Dim itms() As String = fileline.Split(CChar("‡"))
                    Dim lvi As New ListViewItem(itms(0))
                    For i = 1 To itms.Length - 1
                        If i > ListView1.Columns.Count - 1 Then Exit For
                        lvi.SubItems.Add(itms(i))
                    Next
                    ListView1.Items.Add(lvi)
                Next
            End If
        End Sub
    End Class
    

    Friday, October 18, 2013 11:04 PM
  • I get that i is not declared

    Friday, October 18, 2013 11:24 PM
  • What lines are you getting the errors on ?
    Friday, October 18, 2013 11:36 PM
  • I'm getting on every line where "i" is located

    Friday, October 18, 2013 11:37 PM
  • Try changing the For loop lines like this, adding the (As Integer) to them

    For i As Integer = 1 To ListView1.Columns.Count - 1

    and

    For i As Integer = 1 To itms.Length - 1

    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Friday, October 18, 2013 11:42 PM
  • I don't know why it isn't working, I've uploaded my project here:

    Proj2

    Can you take a look please

    Saturday, October 19, 2013 12:03 AM
  • I don't know why it isn't working, I've uploaded my project here:

    Proj2

    Can you take a look please


    Your code is compilable here, even with Option Infer Off.

    Armin

    Saturday, October 19, 2013 12:08 AM
  • I don't know why it isn't working, I've uploaded my project here:

    Proj2

    Can you take a look please

    After looking at your code i have spotted a few things you should fix. The first is at the top of the code where you declare the strFileName and assign the pathname to it. The \ character is missing between the directory and the filename. You should use Path.Combine to add the filename to the directory like this

        Dim strFileName As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "test-k.txt")
    

    The next thing is your frmKupci_Load event sub is missing (Handles MyBase.Load) at the end of it. It should look like this

    Private Sub frmKupci_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    I am taking it that Button1 is to add the 7 textboxes text to each column of the listview. It should be something like this

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim newItm As New ListViewItem(TextBox1.Text)
            newItm.SubItems.Add(TextBox2.Text)
            newItm.SubItems.Add(TextBox3.Text)
            newItm.SubItems.Add(TextBox4.Text)
            newItm.SubItems.Add(TextBox5.Text)
            newItm.SubItems.Add(TextBox6.Text)
            newItm.SubItems.Add(TextBox7.Text)
            List.Items.Add(newItm)
        End Sub
    

    After fixing that your code seemed to work fine. I did not run your project in VB, i just copied the code from Form1 and pasted it into a new project so i did not know if you have the ListView`s View property set to Details but, that needs to be set if you don`t already.

    • Marked as answer by BeAwareAlien Saturday, October 19, 2013 1:15 AM
    Saturday, October 19, 2013 12:48 AM
  • The problem is on load it dosent make them fill the columns, it just fills first columns rest of them are not filled. ( I moved the load from button to form_load).

    Thank you Iron and Armin for your help many thanks.


    Can you just tell me how can I delete selected item (row) ?
    Saturday, October 19, 2013 1:14 AM
  • The problem is on load it dosent make them fill the columns, it just fills first columns rest of them are not filled. ( I moved the load from button to form_load).

    Thank you Iron and Armin for your help many thanks.


    Can you just tell me how can I delete selected item (row) ?

     Your Welcome for the help.  :)

     The code you have for removing the selected items in the project you gave a link to seemed to work alright.

    Saturday, October 19, 2013 1:27 AM