Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:


I use a ListBox in a VB6 program to view a list of calculations performed
by the program. But when the file is re-opened, the list of calculation is lost. I want to save the list in a file. Anybody can help with simple source code. Thanks.

1 Solution
'Add a ListBox control to your form called 'List1'

Private Sub Form_Load()
    'add some sample data
    List1.AddItem "blah1"
    List1.AddItem "blah3"
    List1.AddItem "blah2"

    'save the entries
    SaveListBox List1
    'clear the contents
    'load the saved entries
    LoadListBox List1
End Sub

Private Sub LoadListBox(LB As ListBox)
Dim Entry As String
    'open the file for reading
    Open App.Path & "\" & LB.Name & ".lst" For Input As #1
        'loop while we're not at the (E)nd (O)f (F)ile
        While Not EOF(1)
            'read in the next line
            Line Input #1, Entry
            LB.AddItem Entry
    'close the file
    Close #1
End Sub
Private Sub SaveListBox(LB As ListBox)
Dim X As Long
    'open the file for writing
    Open App.Path & "\" & LB.Name & ".lst" For Output As #1
        'loop through all list entries
        For X = 0 To LB.ListCount - 1
            'Write the entry to file
            Print #1, LB.List(X)
    'close the file
    Close #1
End Sub

Disclaimer: I did not test this code out, if there's a bug let me know and I'll tell you how to fix it if you don't know how to fix it yourself.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now