Results 1 to 15 of 15

Thread: [Resolved] ComboBox Add/Save

  1. #1

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    [Resolved] ComboBox Add/Save

    I need people to be able to Add items to the ComboBox and then it will save it after adding so when it is reloaded it is still there! I just want it to save to the settings or something. No db or anything.
    Last edited by Rivkah; Jul 7th, 2010 at 03:25 PM.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    Hey,

    Have a look at the link in my signature about My.Settings.

    You should be able to use this to store the items that are contained within your ComboBox, and reload them from My.Settings when your form loads again.

    Gary

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ComboBox Add/Save

    Add a StringCollection to your Settings and then you can use that to store your ComboBox items. It's a collection like any other, so it has Clear and Add methods, etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    Thinking about it, another way of doing it would be to create a class that represents the thing that you want to store, create a List(Of thing), make this serializable, and then set this as the DataSource for the ComboBox.

    A general discussion about serialization can also be found in my signature.

    Gary

  5. #5

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: ComboBox Add/Save

    well from what i understood, I could do this exactly like I save a textbox. so I made a button with the code:
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         My.Settings.ComboBox1 = ComboBox1.Items
    3.         'and Etc...
    4.         My.Settings.Save()
    5.     End Sub
    but I get the error:
    Code:
    Value of type 'System.Windows.Forms.Combobox.ObjectCollection' cannot be converted to 'System.Collections.Specialized.String.Collection'.
    Oh, and the setting i did was:
    vb Code:
    1. Name: ComboBox1
    2. Type: System.Collections.Specialized.StringCollection
    3. Scope: User
    4. Value:
    Incase you alrdy didnt know: I am not very good with VB ><

  6. #6

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: ComboBox Add/Save

    /bump?

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    Hey,

    The error message that you are getting is pretty self explanatory.

    You have a System.Collections.Specialized.StringCollection, your My.Setting to hold the items, and you are trying to put a System.Windows.Forms.Combobox.ObjectCollection into it. There is no direct conversion possible between these two types.

    Instead, you are going to have to do something like this when you save:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If My.Settings.MyComboItems Is Nothing Then
                My.Settings.MyComboItems = New System.Collections.Specialized.StringCollection()
            End If
    
            For Each comboItem As Object In Me.ComboBox1.Items
                My.Settings.MyComboItems.Add(comboItem)
            Next
            My.Settings.Save()
        End Sub
    And this when you load them back up again:

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            For Each comboItem As String In My.Settings.MyComboItems
                Me.ComboBox1.Items.Add(comboItem)
            Next
        End Sub
    Gary

  8. #8
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: ComboBox Add/Save

    I advise you to use an XML-file. I also used to use the Settings, but after a while I got a bit sick of the fact that the Settings is overwritten after a new build. Now I use an XML-file and it works really great.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  9. #9

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: ComboBox Add/Save

    How would I do that XML save?...im not any good at VB ><

    Oh, and any way to get it to load setting on start?
    Sorry if im asking more than one thing at once..Just trying to learn all I can
    Last edited by Rivkah; Jul 7th, 2010 at 03:08 PM.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    In order to get it to load on startup, put the code that I showed you in post #7 into the main form's load event.

    If you are just starting out, which it sounds like you are, I would recommend that you stick with My.Settings for now, until you are a bit more comfortable.

    Then you can look into creating your own XML file, using the XmlDocument class:

    http://msdn.microsoft.com/en-us/libr...ldocument.aspx

    Or, using custom class serialization that I mentioned earlier, and which is linked to in my signature.

    Gary

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    Quote Originally Posted by Radjesh Klauke View Post
    I advise you to use an XML-file. I also used to use the Settings, but after a while I got a bit sick of the fact that the Settings is overwritten after a new build. Now I use an XML-file and it works really great.
    Can you confirm what exactly you mean by this? Once your application is installed, the XML file that is generated by your application for My.Settings will be placed within the users documents and settings folder, and will not be overwritten.

    In fact, I have just re-built the application that I knocked up to produce the code in post #7, and the settings are still in tact, so I am not sure what you are referring to.

    Gary

  12. #12

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: ComboBox Add/Save

    Tyvm! I did:
    vb Code:
    1. Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         For Each comboItem As String In My.Settings.MyComboItems
    3.             Me.ComboBox1.Items.Add(comboItem)
    4.         Next
    5.     End Sub
    and it worked perfectly on restart. Yea, don't wanna get ahead of my self. This is meh first program, just putting alot of different items on it to learn.

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ComboBox Add/Save

    Hey,

    Glad to hear that you got it doing what you want.

    Can you remember to mark your thread as resolved?

    Gary

  14. #14

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: ComboBox Add/Save

    Oh sry, will do

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ComboBox Add/Save

    Quote Originally Posted by Radjesh Klauke View Post
    I advise you to use an XML-file. I also used to use the Settings, but after a while I got a bit sick of the fact that the Settings is overwritten after a new build. Now I use an XML-file and it works really great.
    I'm not quite sure what you were doing but settings are not overwritten on a build by default. That's why the Synchronize button exists on the Settings page: to delete the user config file(s) and restore User-scoped settings to their default values, which are stored in the primary config file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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