Results 1 to 5 of 5

Thread: My.Settings save Combobox Items?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Resolved My.Settings save Combobox Items?

    Hello! I have been working on code and wanna store Combo Box items in Application settings since that seems easiest. I have made the variable I made in the settings(comiclist) as Strings Collection basically. Anyways, when the user first loads the application, I have a form that comes up and allows the user to input values to populate the combobox. The code I have for them saving the values first is:
    Code:
        Private Sub txtData_KeyDown(sender As Object, e As KeyEventArgs) Handles txtData.KeyDown
            If (e.KeyCode = Keys.Enter) Then
                 My.Settings.ComicList.Add(txtdata.Text)
            End If
        End Sub
    But the error I get is 'Object reference not set to an instance of an object.'
    I figured to add the .ToString() at the end of My.Settings.ComicList.Add(txtdata.Text) but nah, that didn't work. This is the first time I've worked with String Collection so anything I am doing wrong?
    Last edited by Dragnorian; May 31st, 2017 at 03:34 PM.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: My.Settings save Combobox Items?

    My.Settings is one of those things I gave up on a long time ago because I was already used to maintaining my own settings files and I kept having to spend time on little warts that didn't make any sense.

    In this case: if you don't pre-populate a StringCollection setting with any values, it has no values. As far as My.Settings is concerned, "no values" means it shouldn't bother making a StringCollection instance, which means it's Nothing by default.

    So to be safe, your application should expect this and protect itself:
    Code:
    Protected Overrides Sub OnShown(e As EventArgs)
        MyBase.OnShown(e)
    
        If My.Settings.TestSetting Is Nothing Then
            My.Settings.TestSetting = New Specialized.StringCollection()
            My.Settings.Save()
        End If
    End Sub
    That's not the only way to do it, but it's a good way to make sure at startup the collection exists.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: My.Settings save Combobox Items?

    Alright, thank you man! May I ask, how did you do that Protected Overrides?
    Last edited by Dragnorian; May 31st, 2017 at 02:44 PM.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: My.Settings save Combobox Items?

    I know that Shown is an event forms implement. The pattern Microsoft follows is that for every event, there is a Protected Overridable On<EventName>() method you can override instead of messing with setting up an event handler on yourself. So I typed "Overrides OnShown" and Intellisense filled in the rest for me.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    95

    Re: My.Settings save Combobox Items?

    Oh. That is interesting. Thank you for the help! It all works good now for that issue. Thanks again mate, have a good day!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width