CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2001
    Posts
    6

    Delete a Record From A ListBox

    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


  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Delete a Record From A ListBox

    Supply the code to rewrite the contents back to the text file. It looks like you have just a small error.

    David Paulson

  3. #3
    Join Date
    Mar 2001
    Posts
    6

    Re: Delete a Record From A ListBox

    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


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Delete a Record From A ListBox

    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

  5. #5
    Join Date
    Mar 2001
    Posts
    6

    Re: Delete a Record From A ListBox

    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.


  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Delete a Record From A ListBox

    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

  7. #7
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Delete a Record From A ListBox

    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





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured