locked
VB: Save User settings after closing form. (ListBox) RRS feed

  • Question

  • Hi there,

    I have a quenstion about when you close the form... and you open it again that you can see what you did the previous time.

    It is for a listbox and I can't find how to do it.

    My code:

    Public Class Favorieten
    
        Dim Wedstrijdnaam As Integer
        Dim Wijdstrijdcode As Integer
        'Save Settings
        Private Sub Form2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
            My.Settings.Wedstrijdnaam = ListBox1.Items.ToString
            My.Settings.Wedstrijdcode = ListBox2.Items.ToString
        End Sub
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.ObjectCollection. = My.Settings.Wedstrijdnaam
            ListBox2.ObjectCollection. = My.Settings.Wedstrijdcode
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Add(TextBox1.Text)
            ListBox2.Items.Add(TextBox2.Text)
        End Sub
    End Class

    My settings:

    I hope some one can help me.

    Thijs

    Saturday, July 1, 2017 6:49 PM

Answers

  • Sorry, I'm a beginner and don't understand what I have to do with the "AddRange" and "StringCollection"

    Then don't use it.  There is no advantage in writing code that is more complicated than needed.

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    My.Settings.Wedstrijdnaam = New Specialized.StringCollection For Each S As String in ListBox1.Items My.Settings.Wedstrijdnaam.Add(S) Next My.Settings.Save() End Sub Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load If My.Settings.Wedstrijdnaam IsNot Nothing Then For Each S As St4rig in My.Settings.Wedstrijdnaam ListBox1.Items.Add(S) Next
    End If End Sub

    You may find that code won't work in the Load event, as the list  box isn't created until the end of that event.  In that case, use the Form Shown event instead. Also, form closing may be better than form closed.

    The Type for Wedstrijdnaam that you select in the Settings table needs to be a specialised string collection, not a string. Do not define it again in the form class - that definition occurs automatically when you add it as a settings variable. You haven't included the code for Wijdstrijdcode but it will follow the same pattern.

    This assumes that your list box items are actually strings (the .ToString was not required).   If they are not strings (the ToString was required) then you need to also convert each item from an object to a string (for the Save) and from a string to an object (for the Restore).  See here for an example:
    https://vbdotnetblog.wordpress.com/examples/data-entry-save-and-restore/


    • Edited by Acamar Saturday, July 1, 2017 10:07 PM fmt
    • Marked as answer by Thijs van TOl Saturday, July 1, 2017 10:38 PM
    Saturday, July 1, 2017 10:06 PM

All replies

  • Choose another type for these settings: System.Collections.Specialized.StringCollection. Then:

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    
       My.Settings.Wedstrijdnaam = New Specialized.StringCollection
       My.Settings.Wedstrijdnaam.AddRange(ListBox1.Items.Cast(Of String).ToArray)
       ' . . .
       My.Settings.Save()
    
    End Sub
    
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
       If My.Settings.Wedstrijdnaam IsNot Nothing Then ListBox1.Items.AddRange(My.Settings.Wedstrijdnaam.Cast(Of String).ToArray)
       ' . . .
    
    End Sub

    Similarly for Wedstrijdcode.


    • Edited by Viorel_MVP Saturday, July 1, 2017 8:54 PM
    Saturday, July 1, 2017 8:54 PM
  • Sorry, I'm a beginner and don't understand what I have to do with the "AddRange" and "StringCollection"

    Now I have this:

    Public Class Favorieten
    
        Dim Wedstrijdnaam As Integer
        Dim Wijdstrijdcode As Integer
        'Save Settings
        Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    
            My.Settings.Wedstrijdnaam = New Specialized.StringCollection
            My.Settings.Wedstrijdnaam.AddRange(ListBox1.Items.Cast(Of String).ToArray)
            ' . . .
            My.Settings.Save()
    
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            If My.Settings.Wedstrijdnaam IsNot Nothing Then ListBox1.Items.AddRange(My.Settings.Wedstrijdnaam.Cast(Of String).ToArray)
            ' . . .
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Add(TextBox1.Text)
            ListBox2.Items.Add(TextBox2.Text)
        End Sub
    End Class

    Errors:

    And are my settings good? (the previous picture)

    Thijs

    Saturday, July 1, 2017 9:11 PM
  • Thijs,

    You know what you ask?

    Or in other words, explain it for instance if you had uses Google. 

    It seems to me you want something which was from 1965 when there where few variables.


    Success
    Cor


    Saturday, July 1, 2017 9:51 PM
  • Sorry, I'm a beginner and don't understand what I have to do with the "AddRange" and "StringCollection"

    Then don't use it.  There is no advantage in writing code that is more complicated than needed.

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    My.Settings.Wedstrijdnaam = New Specialized.StringCollection For Each S As String in ListBox1.Items My.Settings.Wedstrijdnaam.Add(S) Next My.Settings.Save() End Sub Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load If My.Settings.Wedstrijdnaam IsNot Nothing Then For Each S As St4rig in My.Settings.Wedstrijdnaam ListBox1.Items.Add(S) Next
    End If End Sub

    You may find that code won't work in the Load event, as the list  box isn't created until the end of that event.  In that case, use the Form Shown event instead. Also, form closing may be better than form closed.

    The Type for Wedstrijdnaam that you select in the Settings table needs to be a specialised string collection, not a string. Do not define it again in the form class - that definition occurs automatically when you add it as a settings variable. You haven't included the code for Wijdstrijdcode but it will follow the same pattern.

    This assumes that your list box items are actually strings (the .ToString was not required).   If they are not strings (the ToString was required) then you need to also convert each item from an object to a string (for the Save) and from a string to an object (for the Restore).  See here for an example:
    https://vbdotnetblog.wordpress.com/examples/data-entry-save-and-restore/


    • Edited by Acamar Saturday, July 1, 2017 10:07 PM fmt
    • Marked as answer by Thijs van TOl Saturday, July 1, 2017 10:38 PM
    Saturday, July 1, 2017 10:06 PM
  • Thanks man.

    Thijs

    Saturday, July 1, 2017 10:38 PM