Results 1 to 9 of 9

Thread: Remove Line From Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2022
    Posts
    9

    Question Remove Line From Text File

    I have 2 textbox :
    Textbox1 to display a line on a text file (barcode.txt)
    Textbox2 for input line/word

    i want to remove line from list of lines, So if the line in textbox2 exists on that list (textbox1), to be remove from list, and file to be saved

    i have a file barcode.txt with list:

    HTML Code:
    qwerty
    asdfgh
    zxcvbg
    If I write in textbox2 : asdfgh and if barcode.txt contains that line then remove it after saving to datagrid. so result need to be :

    HTML Code:
    qwerty
    zxcvbg
    and on textbox1 it always displays the available lines in barcode.txt. how i do it?
    help would be greatly appreciated. Thank you

    Name:  hip.jpg
Views: 386
Size:  23.3 KB
    Attached Images Attached Images  

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Remove Line From Text File

    Try this...

    Code:
    Dim lines As New List(Of String)(IO.File.ReadAllLines("barcode.txt"))
    lines.RemoveAll(Function(l) l.Contains(TextBox2.Text))
    TextBox1.Lines = lines.ToArray
    Edit: Actually, I missed the purpose of TextBox1. The first two lines in my code removes the lines you specify in TextBox2. After that you should save lines back to your file, and probably reload your grid...
    Last edited by .paul.; Feb 11th, 2022 at 12:03 AM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove Line From Text File

    You can do this in one line if you want:
    vb.net Code:
    1. File.WriteAllLines(filePath, File.ReadAllLines(filePath).Where(Function(line) Not line.Contains(TextBox2.Text)))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2022
    Posts
    9

    Lightbulb Re: Remove Line From Text File

    Quote Originally Posted by jmcilhinney View Post
    You can do this in one line if you want:
    vb.net Code:
    1. File.WriteAllLines(filePath, File.ReadAllLines(filePath).Where(Function(line) Not line.Contains(TextBox2.Text)))
    Thanks you

    how to implementation this in my program?
    it's just edit "filepath" with my txt file?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Remove Line From Text File

    I wouldn't recommend doing that in one line, as you also want to reset TextBox1.Lines before saving lines to "barcode.txt"
    How you refer to "barcode.txt" depends on where it is in relation to your executable

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove Line From Text File

    I think I may have misunderstood what you want to do but I can't really see what the purpose of TextBox1 is. Are you saying that you want to remove just the line displayed in TextBox1 if it contains the text displayed in TextBox2?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2022
    Posts
    9

    Re: Remove Line From Text File

    Quote Originally Posted by jmcilhinney View Post
    I think I may have misunderstood what you want to do but I can't really see what the purpose of TextBox1 is. Are you saying that you want to remove just the line displayed in TextBox1 if it contains the text displayed in TextBox2?
    yes, this is i want

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2022
    Posts
    9

    Re: Remove Line From Text File

    Quote Originally Posted by .paul. View Post
    I wouldn't recommend doing that in one line, as you also want to reset TextBox1.Lines before saving lines to "barcode.txt"
    How you refer to "barcode.txt" depends on where it is in relation to your executable
    thank you, i appreciate it

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove Line From Text File

    So how do you get the line in TextBox1 in the first place? It looks like you have a scrollbar in the screen shot. Is it a multiline TextBox that contains the entire file but it's only tall enough to show one line at a time? Please provide a FULL and CLEAR explanation of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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