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

    deleting a particular name in a .txt file

    well, i have a .txt file called Names.txt
    inside this .txt file i stored many names of fruits.
    e.g
    contents in this Names.txt file:
    apple
    orange
    mango
    banana


    and i loaded the fruits names into a listbox called lstNames during form load at run-time.

    during run-time i can delete away the selected fruit name in the listbox using the code:


    private Sub cmdDelete_Click()
    If lstNames.ListIndex >= 0 then
    lstNames.RemoveItem lstNames.ListIndex
    End If
    End Sub




    now, my problem is that, how can i delete the selected fruit name in the .txt file(Names.txt) as well?



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

    Re: deleting a particular name in a .txt file

    Here is the code for you.


    private Sub cmdDelete_Click()
    Dim i as Integer
    Open App.Path & "\names.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 & "\names.txt" for input as #1
    Do While Not EOF(1)
    Line input #1, aLine
    List1.AddItem aLine
    Loop
    Close #1
    End Sub





  3. #3
    Join Date
    Mar 2001
    Posts
    44

    Re: deleting a particular name in a .txt file

    thanks...


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