dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    How to delete a line in txt file by selecting it on listbox

    I have a form with list1
    In the listbox comes the text from a .txt file.

    Example: there are 12 lines en i will remove one of them
    So my question:

    How to delete a line in txt file by selecting it on listbox

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,418

    Re: How to delete a line in txt file by selecting it on listbox

    You will have to rewrite the file based upon the revised list items.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: How to delete a line in txt file by selecting it on listbox

    Thanks.. but how.
    I'm a newby en tries to learn and understand it..

  4. #4
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,076

    Re: How to delete a line in txt file by selecting it on listbox

    How are you reading in the file now? Post the code you are using. That may give us some ideas on the method to suggest for writing out the updated file.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: How to delete a line in txt file by selecting it on listbox

    This is what i use, i have help from 4x2Y

    Private Sub Form_Load()
    On Error Resume Next
    Dim strGame() As String
    Dim j As Long
    Dim i As Long
    Form2.Hide
    mstrGames = ReadAllLines(App.Path & "\game list.txt")
    For j = 0 To UBound(mstrGames)
    If LenB(Trim$(mstrGames(j))) > 0 Then 'Sla over indien geen text meer
    strGame = Split(mstrGames(j), "|")
    List1.AddItem strGame(0) ' game name

    End If
    Next
    List1.ListIndex = 0

    End Sub

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,418

    Re: How to delete a line in txt file by selecting it on listbox

    Simple example:

    Code:
    Option Explicit
    
    Dim myline As String
    Dim intfile As Integer, outfile As Integer
    
    
    Private Sub cmdDelete_Click()
        Dim x As Integer
        List1.RemoveItem (List1.ListIndex)
        outfile = FreeFile
        Open App.Path & "\TopTen.txt" For Output As #outfile
        For x = 0 To List1.ListCount - 1
            List1.ListIndex = x
            Print #outfile, List1.Text
        Next x
        Close #outfile
    End Sub
    
    
    Private Sub Form_Load()
        intfile = FreeFile
        Open App.Path & "\TopTen.txt" For Input As intfile
        Do While Not EOF(intfile)
            Line Input #intfile, myline
            List1.AddItem (myline)
        Loop
    Close #intfile
    End Sub
    uses a Listbox (list1) and a command button (cmdDelete) as well as a .txt file

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    62

    Re: How to delete a line in txt file by selecting it on listbox

    Yes.. this works..
    It might be easy for you, but I did not come up with it.
    Thank you

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,418

    Re: How to delete a line in txt file by selecting it on listbox

    Just think each problem through, slowly. Read your book(s). Search for similar examples on-line. THEN, come to the forum for assistance if you can't figure it out.

    In your case (this example), you first, of course have to know how to open a text file. Then, how to read that file in (in this case example, line by line), so you can add it to a listbox. Then, look up ListBox in MSDN (help) and see how to use it, especially, in this case, how to delete a line. Then once that is done, research how to WRITE to a text file, going line by line (in this example) as you traverse through the listbox.

    The use of MSDN can not be understated as your first line of defense. Use it. If you don't have it installed on your computer, the On-line version will probably suffice in most cases.

    Good luck in your programming 'career'.

    Sammi

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.