Click to See Complete Forum and Search --> : Delete a Record From A ListBox
bdcanetti
March 23rd, 2001, 11:59 PM
I am trying to delete a record from a txt file I have created in NotePad. I populate a List Box with the existing data and then I need to be able to click on the record and delete it from both the list box and the file. I was able to get the delete to work in the list box but no matter where the record was down in the box the first record of the file would delete. So if I click on the fourth record down only the first record in the file will delete. I am pretty new to VB so please help me and don't hesitate to over-explain.
Thank You
I am using VB 6.0
d.paulson
March 24th, 2001, 07:51 AM
Supply the code to rewrite the contents back to the text file. It looks like you have just a small error.
David Paulson
bdcanetti
March 24th, 2001, 11:20 AM
Here is the code but now with it like this it's deleting all the records EXCEPT the first one. The list box acts correctly but once the file is updated only the first record remains.
THANK YOU SO MUCH. IT'S DRIVING ME NUTS!!
Private Sub cmdDelete_Click()
Dim phoneinfo As String
Dim number As String
Dim person As String
Open "c:\windows\temp\PhoneNumbers.txt" For Input As #1
Open "Temp" For Output As #2
Do Until EOF(1)
Input #1, person, number
If lstDisplay.ListIndex <> -1 Then
lstDisplay.RemoveItem (lstDisplay.ListIndex)
txtName.Text = ""
txtNumber.Text = ""
Write #2, person, number
End If
Loop
Close #1
Close #2
Kill "c:\windows\temp\PhoneNumbers.txt"
Name "temp" As "c:\windows\temp\PhoneNumbers.txt"
End Sub
John G Duffy
March 24th, 2001, 07:09 PM
Your write statement is misplaced. If a LstDisplay item is selected when entering the routine, It will be deleted from the Listbox and that first record will be written to File #2, Since you now deleted the selected record, ListIndex now becomes -1 and since the Write #2 within the IF .. END IF it will never be executed again.
Move the Write #2 outside the If .. End IF structure like so
If lstDisplay.ListIndex <> -1 then
'
'
'
'
End if
Write #2, person, number
or posibly
If lstDisplay.Listindex <> -1 then
'
'
'
else
Write #2, person, number
end if
John G
bdcanetti
March 24th, 2001, 09:54 PM
If I move the WRITE statement outside the ENDIF the list box deletes the record but the file isn't updated. If i I try
ELSE
WRITE ........
ENDIF
I'm back to the original problem of only the first record in the file deleting.
Thanks for the try and PLEASE someone help.
John G Duffy
March 25th, 2001, 02:15 PM
Why are you depending the Listindex to equal -1 to determine what to do?
It would seem to me you would compare the selected data in the listbox to the incoming record and if they match to ignore the record otherwise write the incoming record fo FIle #2
John G
shree
March 25th, 2001, 09:00 PM
We must prevent some programmer from going nuts. So, here is the program that will do what you want. The logic in your code seems to have a flaw.
private Sub cmdDelete_Click()
Dim i as Integer
Open App.Path & "\phones.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 & "\phones.txt" for input as #1
Do While Not EOF(1)
Line input #1, aLine
List1.AddItem aLine
Loop
Close #1
End Sub
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.