Write contents of listbox to text file/populate listbox from text file 
Author Message
 Write contents of listbox to text file/populate listbox from text file

Hi
I working on an app and wish to to save the contents of listbox to a text
file. Could some please explain how I achieve this and also how to populate
the listbox from the same file.

Many Thanks

Peter



Wed, 18 Jun 1902 08:00:00 GMT  
 Write contents of listbox to text file/populate listbox from text file
Peter,

Create a new project, it should have one form in it. Put a ListBox and four
CommandButtons on it. Set the Name-property to lstList, cmdFill, cmdSave,
cmdClear and cmdLoad.
Past the following code to the declaration section of the form:

=============================
Option Explicit

Private Sub cmdClear_Click()
    'Make the list empty
    lstList.Clear
End Sub

Private Sub cmdFill_Click()
    'Fill the list with some info
    lstList.AddItem "1 One"
    lstList.AddItem "2 Two"
    lstList.AddItem "3 Three"
    lstList.AddItem "4 Four"
    lstList.AddItem "5 Five"
    lstList.AddItem "6 Six"
    lstList.AddItem "7 Seven"
    lstList.AddItem "8 Eight"
    lstList.AddItem "9 Nine"
    lstList.AddItem "10 Ten"
End Sub

Private Sub cmdLoad_Click()
Dim f As Integer
Dim s As String
    'First make the list empty
    lstList.Clear
    f = FreeFile
    'Open a file for reading
    Open "C:\SaveFile.txt" For Input As f
    Do Until EOF(f)
        Line Input #f, s
        lstList.AddItem s
    Loop
    Close f
End Sub

Private Sub cmdSave_Click()
Dim Ind As Integer
Dim f As Integer
    f = FreeFile
    'Open a file for writing
    Open "C:\SaveFile.txt" For Output As f
    For Ind = 0 To lstList.ListCount - 1
        Print #f, lstList.List(Ind)
    Next
    Close f
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Len(Dir("C:\SaveFile.txt")) Then
        If MsgBox("Do you want to delete the temporary save-file?", vbYesNo
+ vbInformation) = vbYes Then
            Kill "C:\SaveFile.txt"
        End If
    End If
End Sub
=============================
Run the project (Step by step) and see what happens.
Hit F1 as many as you want/need. :-)

Johan.



| Hi
| I working on an app and wish to to save the contents of listbox to a text
| file. Could some please explain how I achieve this and also how to
populate
| the listbox from the same file.
|
|
| Many Thanks
|
|
| Peter
|
|



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Populating listbox with values from a text file

2. Populate a listbox with entries from a text file

3. Looping through text file to load contents in listbox

4. Adding contents of text file in listbox

5. Populate Recipient filed with contents of text file

6. Text File to VB Object (ListBox or Text)

7. Reading from a text file and writing toa text file from Vis Bas 6.0

8. Populating ComboBox with contents of a text file?

9. Writing Cell Contents to Text File

10. Write FileListBox-content to a text-file

11. please help , i need to output the contents of a listbox to a text fil , TIA

12. Usering script to display the contents(text,value) of the option of a html listbox

 

 
Powered by phpBB® Forum Software