[RESOLVED] [2008] combobox add items
I want to add items to a combobox at runtime with text from a textbox, thats easy enough and works great until I shut the program down and restart it. How do you code user input like you would in the string collection property so its permanent. All I got is:
combobox1.items.add(tbxItems.text)
Re: [2008] combobox add items
You have to save it yourself. It's just like anything else in RAM (well almost), it goes away when when the program is closed.
So, you have to choose how you'd like to save it and then work on writing code to do so.
Re: [2008] combobox add items
You should add a StringCollection to your application settings. You can then load its contents into the ComboBox at startup and save the ComboBox's contents into it at shutdown.
Re: [2008] combobox add items
Why not use a StreamWriter?
(Imports System.IO)
Code:
Dim sComboChoice As String = "Some Name" 'Set this to the value of the combo box item that has been added
Using sw As New StreamWriter("Path", True)
sw.WriteLine(sComboChoice)
End Using
Code:
Dim sComboBoxItems As New List(Of String)
Using sr As New StreamReader("Path")
Do Until sr.EndOfStream
sComboBoxItems.Add(sr.ReadLine)
Loop
End Using
cbMyComboBox.Items.AddRange(sComboBoxItems)
Cheers
Re: [2008] combobox add items
Quote:
Originally Posted by Icyculyr
Why not use a StreamWriter?
(Imports System.IO)
Code:
Dim sComboChoice As String = "Some Name" 'Set this to the value of the combo box item that has been added
Using sw As New StreamWriter("Path", True)
sw.WriteLine(sComboChoice)
End Using
Code:
Dim sComboBoxItems As New List(Of String)
Using sr As New StreamReader("Path")
Do Until sr.EndOfStream
sComboBoxItems.Add(sr.ReadLine)
Loop
End Using
cbMyComboBox.Items.AddRange(sComboBoxItems)
Cheers
If you were going to use your own text file then you'd do this:
vb.net Code:
myComboBox.Items.AddRange(File.ReadAllLines("file path here"))
Re: [2008] combobox add items
Hehe, and that returns a string array?
Cool, I'll have to remember that :D
Cheers
Re: [2008] combobox add items
ya I was playing with list (of T), add, and addrange but wasn't in the right direction, I'll try your guy's code when I get home from work.
Re: [2008] combobox add items
It would seem that if you added a small database to your program, you could use that to store just about anything you wanted to store, and then easily repopulate any control you have at runtime with items previously created.
Re: [2008] combobox add items
Depending if you need to dynamically change the list of items, you could use My.Settings to store your items to add. Also, an ini file will work too if adding a database is overkill for your needs.
Re: [2008] combobox add items
The code worked, I do like the database idea which I added one but confused about complex binding. I attached an MS access DB for storing the the combobox selection which works. Do I need a separate table to store the collection for the combobox and the original table to save the selection? Or can a user at runtime create any collection they want and I have to code it using a selectedindex and addrange property or some other way? I'm not sure how to approach this. Can a field save a list of items or one item at a time. If it can save a list, i'm not sure how a field can save a list and not be overwritten by one item selected in the combobox when you save the record. I've looked at several examples but they all address certain steps and not the big picture. I basically want a txtbox, combobox, a button, and a database where the user can enter text in the txtbox which permantly adds the string to the combobox list, then select the item in the combobox and save it to the dB for that record. Is there a tutorial or a walkthrough that someone can point me to, to help me set the proper foundation for a starting point? Or I probably have in this forum and too frazzled to notice the answer staring me in the face as I've probably read every post that has to do with complex binding with listboxes and comboboxes.