Results 1 to 5 of 5

Thread: Saving ListBox items to a file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Question

    I'm making a program that contains a listbox where you can enter a combination of 3 letters and 3 numbers into one line. Since that's not the point lets keep going. I've tried many ways to make it save the ListBox data to a file. Not only that but it also has to load the data when the program starts up. How do I do that? Just for info, the user can only enter a maximum of 50 combinations, but he could enter less if he wishes to!
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  2. #2
    Guest
    This is all you will need to load and save a listbox to a file.

    Code:
    Public Sub List_Add(List As ListBox, txt As String)
    List.AddItem txt
    End Sub
    
    Public Sub List_Load(TheList As ListBox, FileName As String)
    On Error Resume Next
    Dim TheContents As String
    Dim fFile As Integer
    fFile = FreeFile
     Open FileName For Input As fFile
       Do
         Line Input #fFile, TheContents$
            Call List_Add(TheList, TheContents$)
       Loop Until EOF(fFile)
     Close fFile
    End Sub
    
    Public Sub List_Save(TheList As ListBox, FileName As String)
    On Error Resume Next
    Dim Save As Long
    Dim fFile As Integer
    fFile = FreeFile
    Open FileName For Output As fFile
       For Save = 0 To TheList.ListCount - 1
          Print #fFile, TheList.List(Save)
       Next Save
    Close fFile
    End Sub

  3. #3
    Guest
    Try this.
    Code:
    Private Sub Command1_Click(Index As Integer)
        'Save the contents
        Open "MyList" For Output As #1
            For I = 0 To List1.ListCount - 1
                Print #1, List1.List(I)
            Next I
        Close #1
    End Sub
    
    Private Sub Command2_Click()
        'Load the Contents
        Open "MyList" For Input As #1
            Do While Not EOF(1)
                Line Input #1, tmp
                List1.AddItem (tmp)
            Loop
        Close #1
    End Sub
    Matthew: Why are you passing through a function just to add to a ListBox? It's much faster if you do it directly
    Code:
    List1.Additem "MyText"


  4. #4
    Addicted Member
    Join Date
    Aug 2000
    Location
    Columbus Ohio
    Posts
    217

    Lightbulb

    Couldn't you just open a file for output(or append) and use a for each listbox index? Then print the text to the file. Then use a
    do while not eof(x)
    input #1,y
    listbox.additem y
    loop
    to open it?
    Chris

    [email protected]
    Windows XP RC2 B2526
    Visual Studio.Net Beta 2
    C++, VB, VB.Net, ASP, PHP

  5. #5
    Guest
    Megatron Matthew: Why are you passing through a function just to add to a ListBox? It's much faster if you do it directly
    I guess you could easily do it using a simple command. I didn't know it slows it down. But I you are probably correct, it does have to call it and that would slow it down by a few milliseconds, when it could easily be directed to. My mistake! Sorry about that.

    Code:
    Public Sub List_Load(TheList As ListBox, FileName As String)
    On Error Resume Next
    Dim TheContents As String
    Dim fFile As Integer
    fFile = FreeFile
     Open FileName For Input As fFile
       Do
         Line Input #fFile, TheContents$
            TheList.Additem (TheContents$)
       Loop Until EOF(fFile)
     Close fFile
    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