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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

'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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.