2 Attachment(s)
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 :
and on textbox1 it always displays the available lines in barcode.txt. how i do it?
help would be greatly appreciated. Thank you
Attachment 183945
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...
Re: Remove Line From Text File
You can do this in one line if you want:
vb.net Code:
File.WriteAllLines(filePath, File.ReadAllLines(filePath).Where(Function(line) Not line.Contains(TextBox2.Text)))
Re: Remove Line From Text File
Quote:
Originally Posted by
jmcilhinney
You can do this in one line if you want:
vb.net Code:
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?
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
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?
Re: Remove Line From Text File
Quote:
Originally Posted by
jmcilhinney
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
Re: Remove Line From Text File
Quote:
Originally Posted by
.paul.
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
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.