2

On my form I have a menu with "file-save". When I click save I want to save particular settings to restore when the form is closed and re-opened. I've done this successfully for text in text-boxes and the checked states of check-boxes, but I'm failing when trying to loop through the items in a list-box. Please see below for what I've tried...

When I click save:

Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) 
      Handles SaveToolStripMenuItem.Click
For Each i In ListBox1.Items()
    My.Settings.ListBox1.Add(i)
Next
My.Settings.Save()
End Sub

When my form loads:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
        Handles Me.Load
For Each i In My.Settings.ListBox1()
    ListBox1.Items.Add(i)
Next
End Sub

I've only been using VB for three days, so apologies if I am missing something simple ha! Thanks for any help!!!

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
user3688529
  • 97
  • 1
  • 8

1 Answers1

0

There is one small glitch with the StringCollection in settings. If you do not seed it with a fake variable then it starts out as Nothing and you cannot add to Nothing. in your form load add this:

' if the collection has not been initialized, do so
If My.Settings.ListBox1 Is Nothing Then
    My.Settings.ListBox1= New System.Collections.Specialized.StringCollection
End If

' now it is safe to use: load strings from Setting -> form listbox
For Each s As String In My.Settings.ListBox1()
    ListBox1.Items.Add(s)
Next

The very first time it runs, there are likely no saved settings, so we have to create the container for them, basically.


Option Strict can be implemented by file, by adding this at the top:

Option Strict On

Or for the project: Project => Properties => Compile: Option Strict is likely to the right (I have 2012). You can also set it as a permanent option (recommended).

Among other things, this will prevent you from plucking variables out of the air and use them without declaring a type (which will lead to errors). For instance:

For Each i In My.Settings.ListBox1()

becomes

For Each s As String In My.Settings.ListBox1()  ' tell the compiler the Type
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thanks! Getting there. I've put your first if statement in the form load, but shouldn't the second for loop go under the menu save and not form load? Anyway this is what I did so that I add the list box items to my settings when I click save. The I tried to reverse your for loop (which I think I did incorrectly anyway) to add to the form load to take the items from settings and return to the list box. This is what I tried in form load: For i As Integer = 0 To My.Settings.ListBox1.Count - 1 ListBox1.Items.Add(My.Settings.ListBox1.Item(i)) Next – user3688529 Jul 01 '14 at 20:37
  • Apologies for the editing of the code in the comments. Also I'm just going by intuition whilst I'm trying to learn the basics. – user3688529 Jul 01 '14 at 20:38
  • you saw a (too) early draft response, your original loop was ok and the code in the answer is different than the draft. but NO, the `If... Is Nothing` goes in form load. The second form load loop is the same as you had except for `As String` - it was fine. (it **is** confusing because you named the Settings list after a UI control) – Ňɏssa Pøngjǣrdenlarp Jul 01 '14 at 20:39