Results 1 to 3 of 3

Thread: Add item on combobox and 'save'

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    7

    Add item on combobox and 'save'

    Hello I added a item in a combobox with a button in VB.net with this command: SelectieRcpt.Items.add("TestRecipe") This item (Testrecipe)is then added in the combobox but not visible in the items collection property in design mode. In runtime it is visible until i leave the form en go back then this item is disappeared. My question is how to 'save' this combobox item in the list

    Can anyone help me?

    Thanks in advance,

    Erik

    Add a item in the combobox with .net

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Add item on combobox and 'save'

    You can't. Nothing you do at run time actually changes the application itself. That's like typing some text into a TextBox at run time and expecting that text to show up in the designer in VS. Why would it? What if multiple people were running your app? Would you expect any change that any of them made to affect your original project?

    What you can do is save any items you add to the list to somewhere external to the executable and then load that list into the ComboBox when you run the application. Possibly the simplest option for that is to use My.Settings and a StringCollection. Your data will be stored in a user config file that is managed by .NET, so you don't have to worry about the actual saving and loading yourself. Try following these steps:

    1. Open the Settings page of the project properties.
    2. Add a setting of type StringCollection with User scope and named after your ComboBox, e.g. ComboBox1Items.
    3. Click the Value field and then the (...) button to edit the setting.
    4. Add any arbitrary text and click OK. Notice that some XML code is added to the Value.
    5. Edit Value again and remove the text, assuming that you want the ComboBox empty by default. If you want some items included by default, add them here.
    6. In the Load event handler of your form, add code like this to load the settings collection into your ComboBox:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     ComboBox1.Items.AddRange(My.Settings.ComboBox1Items.Cast(Of String)().ToArray())
    3. End Sub
    7. In the FormClosing event handler of your form, add code like this to save the items from the ComboBox back to the settings collection:
    vb.net Code:
    1. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    2.     My.Settings.ComboBox1Items.Clear()
    3.     My.Settings.ComboBox1Items.AddRange(ComboBox1.Items.Cast(Of String)().ToArray())
    4. End Sub
    You can now use the debugger to see the data going back and forth between the control and the setting.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    7

    Re: Add item on combobox and 'save'

    I'm not that experiend but i understand what you propose. I'll test it.
    Thanks for the help

    Erika

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