|
-
Apr 3rd, 2008, 09:27 PM
#1
Thread Starter
Lively Member
[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)
-
Apr 3rd, 2008, 09:33 PM
#2
Frenzied Member
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.
-
Apr 3rd, 2008, 10:17 PM
#3
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.
-
Apr 3rd, 2008, 11:04 PM
#4
Frenzied Member
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
-
Apr 3rd, 2008, 11:40 PM
#5
Re: [2008] combobox add items
 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"))
-
Apr 3rd, 2008, 11:46 PM
#6
Frenzied Member
Re: [2008] combobox add items
Hehe, and that returns a string array?
Cool, I'll have to remember that 
Cheers
-
Apr 4th, 2008, 06:17 AM
#7
Thread Starter
Lively Member
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.
-
Apr 4th, 2008, 07:03 AM
#8
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.
-
Apr 4th, 2008, 07:12 AM
#9
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 5th, 2008, 10:57 AM
#10
Thread Starter
Lively Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|