Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I try to create code for remove specific line where is text in text file based on texbox1.

I have this code but not function, program return me error at sb.remove(line)

Can you help me?

What I have tried:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

      Dim sb As New System.Text.StringBuilder
      For Each line In IO.File.ReadLines("C:\_Montix a.s. - cloud\iMontix\aaa.txt")
          If (line.StartsWith(TextBox1.Text)) Then
              sb.Remove(line)
          End If
      Next






  End Sub
Posted
Updated 5-Mar-18 23:25pm
v2

1 solution

You do not put any text in your StringBuilder object, so there is nothing to remove. And even if you did, the object is not used elsewhere so it serves no purpose. Your logic is the wrong way round it should be:
VB
Dim sb As New System.Text.StringBuilder
For Each line In IO.File.ReadLines("C:\_Montix a.s. - cloud\iMontix\aaa.txt")
    If (Not line.StartsWith(TextBox1.Text)) Then
        sb.Append(line) ' add lines that are not in the textbox
    End If
Next
' store the resultant string somewhere
 
Share this answer
 
Comments
Member 13711215 6-Mar-18 5:45am    
Ok, thank you for fix, but I still cant remove a line and. Nothing happen.

Public Sub button2_click(sender As Object, e As EventArgs) Handles Button2.Click


Dim sb As New System.Text.StringBuilder
For Each line In IO.File.ReadLines("C:\_Montix a.s. - cloud\iMontix\aaa.txt")
If (line.StartsWith(TextBox1.Text)) Then
sb.Append(line) ' add lines that are not in the textbox
End If
Next


End Sub
Richard MacCutchan 6-Mar-18 6:04am    
Well you have not even copied the code I gave you, which is surely not that difficult. You also need to do something with the resulting StringBuilder object; as it stands you just throw it away at the end of the subroutine. What exactly is your program supposed to be doing?
Member 13711215 6-Mar-18 6:52am    
I would like to scan a barcode to textbox1 eg: 00964801000025 and this barcode find in text file aaa.txt and delete line where was this barcode found.
Richard MacCutchan 6-Mar-18 8:41am    
That is a rather different issue. If you want to remove a line of text from a file then you just need to read the file, copying each line to a new one. Check each line as you read it, and if it matches your text string do not write it to the new file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900