Deleting entry from listbox that is logged in txt
Hello, I have a listbox that writes to a txt document when items are added, I just added a delete button with;
Code:
List1.RemoveItem (List1.ListIndex)
List1.Refresh
However, it does not tell the txt file to delete the entry of the same name, so I was wondering how I could do that
Thanks!
Re: Deleting entry from listbox that is logged in txt
So you want to delete the entries from the listbox AND the text file, do you?
Deleting the entry from the text file depends on how you are writing the entries to the file. e.g. are you writing each list entry in a separate line or are you using commas to separate them or what. This issue is must to know before I can post some code that would do the job.
Re: Deleting entry from listbox that is logged in txt
Separated by new line.
It already has the ability to delete from the listbox-only, but it doesn't delete from the txt file, therefore when you reload the program the entry is still there.
Thanks, I appreciate it! :-)
Re: Deleting entry from listbox that is logged in txt
How do the entries in the text file get in the text file?
Are they from the listbox?
If so, then when you delete the item from the listbox, delete the entire text file, and just recreate it with the existing items in the listbox.
Re: Deleting entry from listbox that is logged in txt
This code is may have some merit. I first build the list box and the text box with the same random integer data. The command button knocks out matching lines one at a time until they are both exhausted.
Code:
Const ListSize = 100, MaxNum = 100
Dim sNum() As String, ListPoint As Integer
Private Sub Command1_Click()
' Remove Item from text box at the list box index
If List1.ListIndex > -1 Then
For I = List1.ListIndex To List1.ListCount - 1
sNum(I) = sNum(I + 1)
Next
Text1.Text = Join(sNum, vbNewLine)
' Delete from list box and reset list box pointer
ListPoint = List1.ListIndex
List1.RemoveItem (List1.ListIndex)
If ListPoint < List1.ListCount Then
List1.ListIndex = ListPoint
Else: List1.ListIndex = List1.ListCount - 1
End If
Else: MsgBox "Nothing to remove"
End If
End Sub
Private Sub Form_Load()
ReDim sNum(ListSize)
' Build sample data in both list box and text box
Randomize
For I = 0 To ListSize - 1
sNum(I) = Int(Rnd * MaxNum) + 1
List1.AddItem sNum(I)
Next
Text1.Text = Join(sNum, vbNewLine)
List1.ListIndex = 0
End Sub