none
Read Rich Text File Line by Line RRS feed

  • Question

  • Is it possible to read a rich text file, line by line. Reason being, I've got a 'help' file that I saved in plain text in this form:
    ******************************
    *begin*
    dog weight

    this help is for dog weight. Assign dog's weight in pounds.
    Example:
    dogweight = 8.5
    *end*

    *begin*
    dog height

    This help is for dog height. Assign dog's height in inches.
    Example:
    dogheight = 22
    *end*
    ************************************

    Now find help for entering "dog height".

    procedure:
    I read each line from help file looking for the string "*begin*"
    if found, I read the next line and compare with the string "dog height"
    if found, I read in subsequent lines to a text box until I reach "*end*
    then I close the file

    I'd like to add formatting and color to the plain text file. So I convert it to rich text. But now I can't read it line by line anymore, apparently because of the formatting information. Is it possible to do without reading the entire file and assigning it to variables for the required manipulations?


    BiggyD

    Tuesday, March 27, 2018 8:58 PM

Answers

  • Hi

    Maybe this code snippet will help. Needs RichTextBox1, TextBox1 (Multiline), Button1 on the Form, and Path set to .rtf file.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Plans\New folder\TestRTF\Testing2.rtf"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.LoadFile(path)
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static line As Integer = 0
        TextBox1.AppendText(RichTextBox1.Lines(line) & vbNewLine)
        line += 1
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, March 27, 2018 9:09 PM
    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 9:08 PM
  • Is it possible to do without reading the entire file and assigning it to variables for the required manipulations?

    There's no point.  Keep the source data in the format that allows you to access it as data (and modify, if required).   Then, when you need to show it (or a portion) to the user, format it for the RTB, 

    You could keep your source data as the original text, or as the parsed values, or as both.   If you keep the parsed values then a simple scheme is to use a custom class, with the class instances in a collection such as a List(Of T) or a dictionary.  There are other options, such as a data table.

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 9:16 PM
  • Well a RichText file contains formatting instructions along with text and images that may be in the file.

    I'm not certain where reading the file is required. If you are going to display a RichText file in a RichTextBox then you can read the lines in the RichTextBox unless you have to read lines from the file for some reason.

    And you could not read specific lines from a RichText file to display only in a RichTextBox with an expectation that somehow formatting will remain.

    It would be nice to know the programming procedure you are using for the user to get help. Regardless you could resize a RichTextBox such that only the lines desired to be shown are displayed within the window possibly but I don't know that it would be easy.

    On the other hand you could have a non-displayed (visible = False) RichTextBox with all the text in it and a displayed RichTextBox that can only display copied/pasted formatted text from the visible set to false RichTextBox possibly but I've never tried that.


    La vida loca

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 10:20 PM
  • This works whether RichTextBox1 is visible or not. Perhaps it is not the preferred method for what you are attempting to do though.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            ComboBox1.Items.Add("dog weight")
            ComboBox1.Items.Add("dog height")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "RTF Files *.RTF|*.RTF"
                    .Multiselect = False
                    .Title = "Load RichTextBox1"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    RichTextBox1.LoadFile(OFD.FileName, RichTextBoxStreamType.RichText)
                End If
            End Using
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If RichTextBox1.Visible = True Then
                RichTextBox1.Visible = False
            Else
                RichTextBox1.Visible = True
            End If
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            RichTextBox2.Clear()
            Dim x As Integer = RichTextBox1.Find(ComboBox1.SelectedItem.ToString, 0, RichTextBoxFinds.WholeWord)
            Dim y As Integer = RichTextBox1.Find("*end*", x, RichTextBoxFinds.WholeWord)
            RichTextBox1.Select(x, y - x)
            RichTextBox1.Copy()
            RichTextBox2.Paste()
        End Sub
    
    End Class

    Text placed into Word then formatted with highlighting and saved as RTF to use with RichTextBox1. Copied and pasted into RichTextBox2 it maintains formatting.

    *begin*
     dog weight
    
     this help is for dog weight. Assign dog's weight in pounds.
     Example:
     dogweight = 8.5
     *end*
    
     *begin*
     dog height
    
     This help is for dog height. Assign dog's height in inches.
     Example:
     dogheight = 22
     *end*

    Image


    La vida loca

    • Edited by Mr. Monkeyboy Tuesday, March 27, 2018 11:08 PM
    • Marked as answer by Biggy D Thursday, March 29, 2018 8:33 PM
    Tuesday, March 27, 2018 10:48 PM
  • Hi Biggy,

    You can also take a look the following code:

    Private Sub FrmRichtext_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim path As String = "D:\TestField\Test12.rtf"
            RichTextBox1.LoadFile(path)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim count1 As New List(Of Integer)
            Dim searchstring1 As String = "*begin*"
            Dim searchstring3 As String = "dog height"
            For i As Integer = 0 To RichTextBox1.Lines.Length - 1
                If RichTextBox1.Lines(i) = searchstring1 Then
                    If RichTextBox1.Lines(i + 1) = searchstring3 Then
                        count1.Add(i + 2)
                    End If
                End If
            Next
            For i As Integer = 0 To count1.Count - 1
                For j As Integer = count1(i) To count1(i) + 3
                    TextBox1.Text += RichTextBox1.Lines(j) & Environment.NewLine
                Next
            Next
        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.

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:14 PM
    Wednesday, March 28, 2018 7:04 AM
    Moderator

All replies

  • Hi

    Maybe this code snippet will help. Needs RichTextBox1, TextBox1 (Multiline), Button1 on the Form, and Path set to .rtf file.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim path As String = "C:\Users\lesha\Desktop\Plans\New folder\TestRTF\Testing2.rtf"
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.LoadFile(path)
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static line As Integer = 0
        TextBox1.AppendText(RichTextBox1.Lines(line) & vbNewLine)
        line += 1
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, March 27, 2018 9:09 PM
    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 9:08 PM
  • Is it possible to do without reading the entire file and assigning it to variables for the required manipulations?

    There's no point.  Keep the source data in the format that allows you to access it as data (and modify, if required).   Then, when you need to show it (or a portion) to the user, format it for the RTB, 

    You could keep your source data as the original text, or as the parsed values, or as both.   If you keep the parsed values then a simple scheme is to use a custom class, with the class instances in a collection such as a List(Of T) or a dictionary.  There are other options, such as a data table.

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 9:16 PM
  • Well a RichText file contains formatting instructions along with text and images that may be in the file.

    I'm not certain where reading the file is required. If you are going to display a RichText file in a RichTextBox then you can read the lines in the RichTextBox unless you have to read lines from the file for some reason.

    And you could not read specific lines from a RichText file to display only in a RichTextBox with an expectation that somehow formatting will remain.

    It would be nice to know the programming procedure you are using for the user to get help. Regardless you could resize a RichTextBox such that only the lines desired to be shown are displayed within the window possibly but I don't know that it would be easy.

    On the other hand you could have a non-displayed (visible = False) RichTextBox with all the text in it and a displayed RichTextBox that can only display copied/pasted formatted text from the visible set to false RichTextBox possibly but I've never tried that.


    La vida loca

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:13 PM
    Tuesday, March 27, 2018 10:20 PM
  • This works whether RichTextBox1 is visible or not. Perhaps it is not the preferred method for what you are attempting to do though.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            ComboBox1.Items.Add("dog weight")
            ComboBox1.Items.Add("dog height")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "RTF Files *.RTF|*.RTF"
                    .Multiselect = False
                    .Title = "Load RichTextBox1"
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    RichTextBox1.LoadFile(OFD.FileName, RichTextBoxStreamType.RichText)
                End If
            End Using
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If RichTextBox1.Visible = True Then
                RichTextBox1.Visible = False
            Else
                RichTextBox1.Visible = True
            End If
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            RichTextBox2.Clear()
            Dim x As Integer = RichTextBox1.Find(ComboBox1.SelectedItem.ToString, 0, RichTextBoxFinds.WholeWord)
            Dim y As Integer = RichTextBox1.Find("*end*", x, RichTextBoxFinds.WholeWord)
            RichTextBox1.Select(x, y - x)
            RichTextBox1.Copy()
            RichTextBox2.Paste()
        End Sub
    
    End Class

    Text placed into Word then formatted with highlighting and saved as RTF to use with RichTextBox1. Copied and pasted into RichTextBox2 it maintains formatting.

    *begin*
     dog weight
    
     this help is for dog weight. Assign dog's weight in pounds.
     Example:
     dogweight = 8.5
     *end*
    
     *begin*
     dog height
    
     This help is for dog height. Assign dog's height in inches.
     Example:
     dogheight = 22
     *end*

    Image


    La vida loca

    • Edited by Mr. Monkeyboy Tuesday, March 27, 2018 11:08 PM
    • Marked as answer by Biggy D Thursday, March 29, 2018 8:33 PM
    Tuesday, March 27, 2018 10:48 PM
  • Hi Biggy,

    You can also take a look the following code:

    Private Sub FrmRichtext_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim path As String = "D:\TestField\Test12.rtf"
            RichTextBox1.LoadFile(path)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim count1 As New List(Of Integer)
            Dim searchstring1 As String = "*begin*"
            Dim searchstring3 As String = "dog height"
            For i As Integer = 0 To RichTextBox1.Lines.Length - 1
                If RichTextBox1.Lines(i) = searchstring1 Then
                    If RichTextBox1.Lines(i + 1) = searchstring3 Then
                        count1.Add(i + 2)
                    End If
                End If
            Next
            For i As Integer = 0 To count1.Count - 1
                For j As Integer = count1(i) To count1(i) + 3
                    TextBox1.Text += RichTextBox1.Lines(j) & Environment.NewLine
                Next
            Next
        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.

    • Marked as answer by Biggy D Tuesday, November 27, 2018 6:14 PM
    Wednesday, March 28, 2018 7:04 AM
    Moderator
  • I guess the answer is, no, you can't get there from here. Loading the entire .rtf file seems to be the only option. So I loaded the .rtf file into an invisible rich text box and used your copy/paste method to copy the text section I needed to a second "visible" rich text box.

    Thank you. It now works perfectly.

    Dave


    BiggyD

    Thursday, March 29, 2018 8:32 PM
  • You're welcome. Parsing the .RTF would not provide the formatting instructions for formatted text. Not without alot of trial and error and overcoming many obstacles IMO.

    La vida loca

    Thursday, March 29, 2018 11:57 PM