How to add an item into a ListBox during runtime-VBForums
Results 1 to 5 of 5

Thread: How to add an item into a ListBox during runtime

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008

    How to add an item into a ListBox during runtime


    I want to add new Items time by time into my ListBox after making exe file. If I put a command button, it places the new Item in the ListBox but it is temporary, it is not stored in the code. How can I make it permanent ?


  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Around the corner from si_the_geek

    Re: How to add an item into a ListBox during runtime

    You need to write the contents either to a file or a database. Simplest would be to write it in a file. If you search the forums, there are many example showing you how to write the contents of a listbox to a file and the corresponding code to read them back

    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    General Santos City, Philippines

    Re: How to add an item into a ListBox during runtime

    Private Sub Form_Load()
    Dim strr As String
    Open App.Path & "\ListFiles.txt" For Input As #2
        Do Until EOF(2)
         Input #2, strr
         List1.AddItem strr
    Close #2
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    Dim i As Integer
    Open App.Path & "\ListFiles.txt" For Output As #1
        For i = 0 To List1.ListCount - 1
            Print #1, List1.List(i)
        Next i
    Close #1
    End Sub
    "More Heads are Better than One"

  4. #4
    Addicted Member
    Join Date
    Apr 2008

    Re: How to add an item into a ListBox during runtime

    list1.additem "item as string"
    IT CTO & System Administrator.

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Searching for mendhak

    Re: How to add an item into a ListBox during runtime

    vb Code:
    1. Private Sub SaveLoadListbox(plstLB As ListBox, _
    2. pstrFileName As String, _
    3. pstrSaveOrLoad As String)
    5. Dim strListItems As String
    6. Dim i As Long
    8. Select Case pstrSaveOrLoad
    9.    Case "save"
    10.     Open pstrFileName For Output As #1
    11.     For i = 0 To plstLB.ListCount - 1
    12.         plstLB.Selected(i) = True
    13.         Print #1, plstLB.List(plstLB.ListIndex)
    14.     Next
    15.     Close #1
    17.    Case "load"
    18.    plstLB.Clear
    19.     Open pstrFileName For Input As #1
    20.     While Not EOF(1)
    21.       Line Input #1, strListItems
    22.       plstLB.AddItem strListItems
    23.     Wend
    24.     Close #1
    25. End Select
    27. End Sub
    29. Private Sub Form_Load()
    30. Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
    31. End Sub
    33. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    34. Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
    35. End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

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

Survey posted by VBForums.