|
-
Jul 29th, 2000, 03:52 PM
#1
Thread Starter
Hyperactive Member
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)
-
Jul 29th, 2000, 04:01 PM
#2
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
-
Jul 29th, 2000, 04:01 PM
#3
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]
-
Jul 29th, 2000, 05:26 PM
#4
Thread Starter
Hyperactive Member
To Matthew, Didn't work.
To Megatron, explain yourself coz I'm just a beginner and I don't know much about VB...
-
Jul 29th, 2000, 05:43 PM
#5
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
-
Jul 30th, 2000, 07:54 AM
#6
Thread Starter
Hyperactive Member
-
Jul 30th, 2000, 07:24 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|