Results 1 to 7 of 7

Thread: 2 Simple Questions

  1. #1

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    1. If I have a listbox named lstDB and a commandbutton named cmdSave, how do I make it that whenever I press on cmdSave, it will save the items on lstDB on a txtfile, but it has to be sorted like this. If on the list I have this:

    Bla
    Bla
    Bla
    Bla2
    Bla2
    Bla3
    Bla3
    Bla3
    Bla3

    then on the TXT I'll have this:

    x3 Bla
    x2 Bla2
    x4 Bla3

    2. How do I open the TXT I saved? (see previous question)
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  2. #2
    Guest
    Code:
    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, "x" & TheList.ListIndex & " " & TheList.List(Save)
       Next Save
    Close fFile
    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_Add(List As Listbox, txt As String)
    List.AddItem txt
    End Sub

  3. #3
    Guest
    To Save:
    Code:
    For I = 0 To List1.ListCount - 1
    
        List1.Selected(I) = True
        
        tmp = I
        If tmp = 0 Then tmp = 1
        
        Open "C:\Windows\Desktop\MyFile.txt" For Append As #1
        For iPrint = 0 To I
            Print #1, List1.Text
        Next iPrint
        Close #1
        
    Next I
    To Load:

    Code:
    Open "C:\Windows\Desktop\MyFile.txt" For Input As #1
        Do While Not EOF(1)
            Line Input #1, tmp
            List1.AddItem tmp
        Loop
    Close #1
    [Edited by Megatron on 07-29-2000 at 05:04 PM]

  4. #4

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    To Matthew, Didn't work.
    To Megatron, explain yourself coz I'm just a beginner and I don't know much about VB...
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  5. #5
    Guest
    Sorry about that.

    To Save
    Code:
    'Loop through each item in the List
    For I = 0 To List1.ListCount - 1
    
        'Select the current item
        List1.Selected(I) = True
        
        'Open the file to write to
        Open "C:\Windows\Desktop\MyFile.txt" For Append As #1
        'Loop through it "I" times. (I is the current number we are at)
        For iPrint = 0 To I
            'Print it in the file
            Print #1, List1.Text
        Next iPrint
        'Close the file
        Close #1
        
    Next I
    To Load
    Code:
    'Open a file to read from
    Open "C:\Windows\Desktop\MyFile.txt" For Input As #1
        'Loop throught the file until we reach the end of file (EOF)
        Do While Not EOF(1)
            'Get 1 line at a time
            Line Input #1, tmp
            'Add that line to the ListBox
            List1.AddItem tmp
        Loop
    'Close the file
    Close #1

  6. #6

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    ThanX, I'll try it out.
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  7. #7
    Guest
    Sorry about that. We all make mistakes...but it is fixed now.

    Code:
    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, "x" & Save & " " & TheList.List(Save)
       Next Save
    Close fFile
    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_Add(List As Listbox, txt As String)
    List.AddItem txt
    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