none
Removing line from text file and inseting to new text file RRS feed

  • Question

  • Hello, Is it possible to read the text file to rich textbox then select specific line and remove and insert that line to another text file. Thank you
    Sunday, January 7, 2018 11:48 PM

Answers

  • Yes I would like to use List box and how to remove that selected item from text file at the same time?

     Ok,  one more shot at this....

     This will do the same but,  will remove the line from the original file too.

    Public Class Form1
        Private OriginalFilePath As String = "C:\TestFolder\MyTextFile.txt"
        Private OtherFilePath As String = "C:\TestFolder\MyOtherTextFile.txt"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(IO.File.ReadAllLines(OriginalFilePath))
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If ListBox1.SelectedIndex > -1 Then
                Using sw As New IO.StreamWriter(OtherFilePath, True)
                    sw.WriteLine(ListBox1.SelectedItem.ToString)
                End Using
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
                IO.File.WriteAllLines(OriginalFilePath, ListBox1.Items.Cast(Of String))
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    Monday, January 8, 2018 8:39 PM

All replies

  • Hello, Is it possible to read the text file to rich textbox then select specific line and remove and insert that line to another text file. Thank you

    Hi

    This may point the way (one of many). Form1 with RichTextBox1.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Plans\New folder\"
      Dim File1 As String = "Testing.txt"
      Dim File21 As String = "Testing21.txt"
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	RichTextBox1.LoadFile(IO.Path.Combine(path, File1), RichTextBoxStreamType.PlainText)
    
    
    	Dim line() As String = {RichTextBox1.Lines(21)}
    
    	IO.File.WriteAllLines(IO.Path.Combine(path, File21), line)
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, January 8, 2018 12:02 AM
    Monday, January 8, 2018 12:01 AM
  • Leshay, thank you for fast response. May I ask what would be other way of doing this, just curious.
    Monday, January 8, 2018 12:23 AM
  • Leshay, thank you for fast response. May I ask what would be other way of doing this, just curious.

    Hi

    Here is another way (I omitted the RTB remove in last code)

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Plans\New folder\"
      Dim File1 As String = "Testing.txt"
      Dim File10 As String = "Testing10.txt"
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	RichTextBox1.LoadFile(IO.Path.Combine(path, File1), RichTextBoxStreamType.PlainText)
    
    	Dim lines As List(Of String) = RichTextBox1.Lines.ToList
    
    	Dim line() As String = {lines(10)}
    
    	IO.File.WriteAllLines(IO.Path.Combine(path, File10), line)
    
    	lines.RemoveAt(10)
    
    	RichTextBox1.Clear()
    
    	For Each ln As String In lines
    	  RichTextBox1.AppendText(ln & vbCrLf)
    	Next
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Monday, January 8, 2018 12:48 AM
  • Since you're using a RichTextBox, you might also consider using a method that's built-in:

    The .SaveFile method let you save it as plain text or as RichText.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, January 8, 2018 12:55 AM
  • Leshay, thank you for fast response. May I ask what would be other way of doing this, just curious.

     And here is yet another version of doing the same thing....

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Lines = IO.File.ReadAllLines("C:\TestFolder\MyTextFile.txt")
        End Sub

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim lines As List(Of String) = RichTextBox1.Lines.ToList
            IO.File.WriteAllLines("C:\TestFolder\MyOtherTextFile.txt", {lines(4)})
            lines.RemoveAt(4)
            RichTextBox1.Lines = lines.ToArray
        End Sub
    End Class
     

     EDIT - Oops.. i did not see you wanted to save only the one line to the other file.  Fixed that.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, January 8, 2018 1:13 AM
    Monday, January 8, 2018 1:06 AM
  •  Are you going to want to append more lines to the end of the other file?  If so,  you will need a different method for saving the file.

     This will do the same thing but,  instead of overwriting the other file,  it will append the line to the end of the other file.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Lines = IO.File.ReadAllLines("C:\TestFolder\MyTextFile.txt")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim lines As List(Of String) = RichTextBox1.Lines.ToList
            Using sw As New IO.StreamWriter("C:\TestFolder\MyOtherTextFile.txt", True)
                sw.WriteLine(lines(4))
            End Using
            lines.RemoveAt(4)
            RichTextBox1.Lines = lines.ToArray
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, January 8, 2018 1:20 AM
    Monday, January 8, 2018 1:15 AM
  • Hi

    Here is a more concise version. Note the LineNumber is set at top of code.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Plans\New folder\"
      Dim File1 As String = "Testing.txt"
      Dim File10 As String = "Testing10.txt"
      Dim LineToDelete As Integer = 10
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    	RichTextBox1.LoadFile(IO.Path.Combine(path, File1), RichTextBoxStreamType.PlainText)
    
    	RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(LineToDelete)
    
    	RichTextBox1.SelectionLength = RichTextBox1.Lines(LineToDelete).Length + 1
    
    	Dim line() As String = {RichTextBox1.SelectedText}
    
    	IO.File.WriteAllLines(IO.Path.Combine(path, File10), line)
    
    	RichTextBox1.SelectedText = String.Empty
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, January 8, 2018 1:47 AM
    Monday, January 8, 2018 1:35 AM
  • Hello, Is it possible to read the text file to rich textbox then select specific line and remove and insert that line to another text file. Thank you

     I guess before we get carried away posting a hundred ways to do this,  we need to make sure we are completely understanding what you want to do.

    1.  You say you want to "remove and insert" that line into another file.  Does that mean you want to remove that line from just the RichTextBox or does that mean you want to remove that line from the original file and the RichTextBox?

    2.  Do you want to append the selected line to the end of the other file,  or do you want to keep overwriting the other file.  Overwriting the other file means the file would only have the last selected line written in it at any given time.

    3.  When you say "select specific line",  does that mean a whole line (left to right) or do you want to select partial lines of text.  Like a single word or sentence in a paragraph?

    4.  What does your file look like?

    5.  Is this a RichText file which contains formatted text,  like different colors and different fonts,  or is it just a Plain Text file?

     These details would help us to help you quicker.  When we have to guess at all these things,  it seems to take longer to get to the correct answer that does exactly what you want.  8)


    If you say it can`t be done then i`ll try it

    Monday, January 8, 2018 2:21 AM
  • Hello, Is it possible to read the text file to rich textbox then select specific line and remove and insert that line to another text file. Thank you

    Hi BloodySandwich,

    Firstly read text file in RichTextBox, then remove the selected line from RichTextBox, finally write this line into another text file.

    Private Sub FrmReadtxtfile_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim path As String = "D:\TestField\Test3.txt"
            RichTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText)
        End Sub
    
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim content As String = RichTextBox1.SelectedText
            Dim path1 As String = "D:\TestField\Test4.txt"
            If File.Exists(path1) = True Then
                Using sw As StreamWriter = File.AppendText(path1)
                    sw.WriteLine(content & Environment.NewLine())
                    sw.Close()
                End Using
                MessageBox.Show("Text written to file")
            Else
                MessageBox.Show("File Does Not Exist")
            End If
            RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.Text.IndexOf(RichTextBox1.SelectedText), RichTextBox1.SelectedText.Length)
    
    
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 8, 2018 6:31 AM
    Moderator
  • Hello, Is it possible to read the text file to rich textbox then select specific line and remove and insert that line to another text file. Thank you

     I guess before we get carried away posting a hundred ways to do this,  we need to make sure we are completely understanding what you want to do.

    1.  You say you want to "remove and insert" that line into another file.  Does that mean you want to remove that line from just the RichTextBox or does that mean you want to remove that line from the original file and the RichTextBox?

    2.  Do you want to append the selected line to the end of the other file,  or do you want to keep overwriting the other file.  Overwriting the other file means the file would only have the last selected line written in it at any given time.

    3.  When you say "select specific line",  does that mean a whole line (left to right) or do you want to select partial lines of text.  Like a single word or sentence in a paragraph?

    4.  What does your file look like?

    5.  Is this a RichText file which contains formatted text,  like different colors and different fonts,  or is it just a Plain Text file?

     These details would help us to help you quicker.  When we have to guess at all these things,  it seems to take longer to get to the correct answer that does exactly what you want.  8)


    If you say it can`t be done then i`ll try it

    Hello, Thank you everyone for their time, sorry everyone for not explaining clear requirements, after n day of 12-15 hours trying to "code" my brain is slowly melting and I didn't think good enough about this problem. What I need is to see the each line of the text file in list box then select than line from text box and send to another text file to the last index so if there is any text, each line wont overwrite another. Thanks
    Monday, January 8, 2018 7:37 PM
  • Hi

    A little better description. I think that IronRazers code posted in this thread earlier does exactly as you describe here.


    Regards Les, Livingston, Scotland

    Monday, January 8, 2018 7:54 PM
  •  Well,  the example below is almost the same thing i posted in my second post in this thread.  However,  it has 1 more line in it to get the index of the line that is selected in the RichTextBox.  This will also work with a TextBox too,  just replace RichTextBox1 with the name of your RichTextBox or TextBox.

     
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RichTextBox1.Lines = IO.File.ReadAllLines("C:\TestFolder\MyTextFile.txt")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If RichTextBox1.SelectionLength > 0 Then
                Dim lines As List(Of String) = RichTextBox1.Lines.ToList
                Dim SelectedLineIndex As Integer = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
                Using sw As New IO.StreamWriter("C:\TestFolder\MyOtherTextFile.txt", True)
                    sw.WriteLine(lines(SelectedLineIndex))
                End Using
                lines.RemoveAt(SelectedLineIndex)
                RichTextBox1.Lines = lines.ToArray
            End If
        End Sub
    End Class
    
    

     

     I see you said "ListBox" in your reply so,  now i am not sure if it is a TextBox,  RichTextBox,  or ListBox that you are using.  So,  the below will work for a ListBox instead of a TextBox or RichTextBox.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(IO.File.ReadAllLines("C:\TestFolder\MyTextFile.txt"))
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If ListBox1.SelectedIndex > -1 Then
                Using sw As New IO.StreamWriter("C:\TestFolder\MyOtherTextFile.txt", True)
                    sw.WriteLine(ListBox1.SelectedItem.ToString)
                End Using
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            End If
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, January 8, 2018 8:06 PM
    Monday, January 8, 2018 8:02 PM
  • Yes I would like to use List box and how to remove that selected item from text file at the same time?
    Monday, January 8, 2018 8:26 PM
  • Yes I would like to use List box and how to remove that selected item from text file at the same time?

     Ok,  one more shot at this....

     This will do the same but,  will remove the line from the original file too.

    Public Class Form1
        Private OriginalFilePath As String = "C:\TestFolder\MyTextFile.txt"
        Private OtherFilePath As String = "C:\TestFolder\MyOtherTextFile.txt"
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(IO.File.ReadAllLines(OriginalFilePath))
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If ListBox1.SelectedIndex > -1 Then
                Using sw As New IO.StreamWriter(OtherFilePath, True)
                    sw.WriteLine(ListBox1.SelectedItem.ToString)
                End Using
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
                IO.File.WriteAllLines(OriginalFilePath, ListBox1.Items.Cast(Of String))
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    Monday, January 8, 2018 8:39 PM