deleting a particular name in a .txt file
well, i have a .txt file called Names.txt
inside this .txt file i stored many names of fruits.
e.g
contents in this Names.txt file:
apple
orange
mango
banana
and i loaded the fruits names into a listbox called lstNames during form load at run-time.
during run-time i can delete away the selected fruit name in the listbox using the code:
private Sub cmdDelete_Click()
If lstNames.ListIndex >= 0 then
lstNames.RemoveItem lstNames.ListIndex
End If
End Sub
now, my problem is that, how can i delete the selected fruit name in the .txt file(Names.txt) as well?
Re: deleting a particular name in a .txt file
Here is the code for you.
private Sub cmdDelete_Click()
Dim i as Integer
Open App.Path & "\names.txt" for Output as #1
for i = 0 to List1.ListCount - 1
If i <> List1.ListIndex then
print #1, List1.List(i)
End If
next
List1.RemoveItem List1.ListIndex
End Sub
private Sub Form_Load()
Dim aLine as string
Open App.Path & "\names.txt" for input as #1
Do While Not EOF(1)
Line input #1, aLine
List1.AddItem aLine
Loop
Close #1
End Sub
Re: deleting a particular name in a .txt file