Results 1 to 10 of 10

Thread: [RESOLVED] [2008] combobox add items

  1. #1

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Resolved [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)

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    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.

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

    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.
    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
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

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

    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:
    1. myComboBox.Items.AddRange(File.ReadAllLines("file path here"))
    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

  6. #6
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] combobox add items

    Hehe, and that returns a string array?

    Cool, I'll have to remember that

    Cheers

  7. #7

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  10. #10

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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
  •  



Click Here to Expand Forum to Full Width