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
    Posts
    13

    How to add an item into a ListBox during runtime

    Hi,

    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 ?

    Engin

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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
    Location
    General Santos City, Philippines
    Posts
    310

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

    try:
    Code:
    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
        Loop
    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
    Posts
    198

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

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

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

    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)
    4.  
    5. Dim strListItems As String
    6. Dim i As Long
    7.  
    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
    16.  
    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
    26.  
    27. End Sub
    28.  
    29. Private Sub Form_Load()
    30. Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
    31. End Sub
    32.  
    33. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    34. Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
    35. End Sub

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