Results 1 to 10 of 10

Thread: [RESOLVED] Can user write to combo box at runtime?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Resolved [RESOLVED] Can user write to combo box at runtime?

    Excuse, but I'm just learning VB5 (to start with) and I'm trying to let a user add items to a combo box during runtime -- and have his inpput actually saved into the drop down list for the next time the program is run.

    Using cboBox.AddItem cboBox.Text saves the text during the present run of the program but then it disappears when the program is terminated - it's not actually saved as a new item in the drop down list for the next run.

    I assume it's kinda basic for you guys, but I've been trying to find the solution for over 2 weeks....it's a huge problem to me.

    Any help on making the user input a permenant combo box item?

    Thank you.

    John

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Can user write to combo box at runtime?

    You have to persist the data to make it come back the next time. The data the user adds is not actually stored in the program it just displays while the program is open. To persist the data you have a few choices:

    1. Save the data to a database and retrieve on the next start of the application.
    2. Save the data to a file and retrieve on the next start of the application.
    3. Save the data to a registry setting and retrieve on the next start of the application.

    So as you can see in all 3 methods you need to save the data some where on application exit and then retrieve that data on application startup.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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

    Re: Can user write to combo box at runtime?

    You should stay away from writting to the registry as if the app is run on Vista it will require Admin perms if not written to the VB and VBA Program Settings.

    Why learn VB5? VB 2008 is free and more advanced.
    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

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Can user write to combo box at runtime?

    Here's #3

    Code:
    Option Explicit
    Private Const SEP = "||"
    Private Sub Command1_Click()
    cboBox.AddItem cboBox.Text
    End Sub
    
    
    Private Sub Form_Load()
    Dim strValues() As String
    Dim intIndex As Integer
    
    strValues = Split(GetSetting(App.EXEName, "UserEntry", "Combobox", ""), SEP)
    
    For intIndex = 0 To UBound(strValues)
        cboBox.AddItem strValues(intIndex)
    Next
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
    
    Dim intIndex As Integer
    Dim strValues As String
    
    For intIndex = 0 To cboBox.ListCount - 1
        strValues = strValues & cboBox.List(intIndex) & SEP
    Next
    strValues = Left$(strValues, Len(strValues) - Len(SEP))
    SaveSetting App.EXEName, "UserEntry", "Combobox", strValues
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Re: Can user write to combo box at runtime?

    Thanks for the info...

    As to writing to a file to save the info and then loading the file at the next runtime, which is best, and would it be too much of a hassle to provide an small example of how to write to a text file within the program and then have it loaded again for the combo box? Confusing to me...

    I'll start looking up how to do it, but an example would be fantastic. If it's too involved for anyone to mess with that's not a problem, I appreciate the help I've already gotten.

    As far as VB 2008, I wasn't aware even of it. Someone gave me a copy of VB 5 so I started playing around with it. I must admit it makes me feel slightly 'stupid' when trying to read and actually understand the 'help' files, but I'm having fun anyway.

    Thanks again.

    John

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Can user write to combo box at runtime?

    Textfile version.

    Code:
    Option Explicit
    Private Const SEP = "||"
    Private Const FILE_NAME = "C:\temp\SavedValues.txt"
    Private Sub Command1_Click()
    cboBox.AddItem cboBox.Text
    End Sub
    
    
    Private Sub Form_Load()
    Dim strValues() As String
    Dim strTemp As String
    Dim intIndex As Integer
    Dim FF As Integer
    
    FF = FreeFile
    
    On Error Resume Next ' In case file doesn't exist
    Open FILE_NAME For Input As #FF
    If Err.Number = 53 Then
        ' File doesn't exist
        Close
        Exit Sub
    End If
    On Error GoTo 0 ' Resume normal error handling
    Line Input #FF, strTemp
    strValues = Split(strTemp, SEP)
    
    For intIndex = 0 To UBound(strValues)
        cboBox.AddItem strValues(intIndex)
    Next
    Close
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
    
    Dim intIndex As Integer
    Dim strValues As String
    Dim FF As Integer
    
    FF = FreeFile
    
    For intIndex = 0 To cboBox.ListCount - 1
        strValues = strValues & cboBox.List(intIndex) & SEP
    Next
    strValues = Left$(strValues, Len(strValues) - Len(SEP))
    Open FILE_NAME For Output As #FF
    Print #FF, strValues
    Close
    End Sub

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Can user write to combo box at runtime?

    Couple tips:
    You may want to put save and refresh buttons by your list to keep your list
    current. When you want to add a new item or rename a item, click save to add it your list and Save-Refresh. To delete a item use RemoveItem and click save and refresh to reload the cbo
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    Re: Can user write to combo box at runtime?

    Wow - okay... I had no idea it was that involved. I appreciate your taking the time to help.

    I'll place the code into the app and keep playing with it until it starts to make sense to me...(no, really, it could happen...).

    Thanks again, it's most appreciated...

    John

  9. #9

  10. #10
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Can user write to combo box at runtime?

    Quote Originally Posted by John_StL
    Wow - okay... I had no idea it was that involved. I appreciate your taking the time to help.

    I'll place the code into the app and keep playing with it until it starts to make sense to me...(no, really, it could happen...).

    Thanks again, it's most appreciated...

    John
    Not only that but sometime you really have to hack these vb controls to make them do what you want. For instance I have a cbo that when items are clicked they are removed from the list and added to a rtf control. (Not permanently) to avoid duplicates. The hack i used was:
    cboMaterials.RemoveItem I
    SendKeys "{F4}" 'Makes the combo stay dropped to allow adding the next item
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

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