Results 1 to 5 of 5

Thread: Deleting entry from listbox that is logged in txt

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    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!

  2. #2
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2008
    Posts
    268

    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! :-)

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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
    Doctor Ed

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