Results 1 to 17 of 17

Thread: Reading a Text File into Multiple RichTextBoxes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Reading a Text File into Multiple RichTextBoxes

    I'm not really sure the best way to go about doing this, so I've come to the forum hoping to get some help. Basically, my program has 5 threads and each thread works with the data that's inside its associated RichTextBox (Thread1 goes off the lines in RichTextBox1...Thread2 goes off the lines in RichTextBox2..etc).

    What I would like to do is load a text file and have the program split that text file up into each RichTextBox in a specific way. If the file has 10 lines of data and the program is running 5 threads then I want line #1 and line #6 put into RichTextBox1, then line #2 and line #7 into RichTextBox2, line #3 and line #8 into RichTextBox3...etc. However, the file might not always have 10 lines of data and I might not always run all 5 threads. At times it might have 7 lines of data and 5 threads selected, or 8 lines of data and 3 threads selected. Basically, the number of lines in the text file will always be different and the number of threads I'm wanting to run can be different as well.

    I figure that in order to do this I need to look at how many lines are in the text file and how many threads are selected to run (1-5). I can divide those two numbers to determine how many lines will go into each RichTextBox, but if it isn't a whole number then that's going to mess things up..

    As you can see from my code below, I'm able to read in the text file and put the lines into an array so I can remove any blank/empty lines. I'm also able to divide these numbers to determine how many will go into each box, but now I'm stuck. I thought putting these items into an array would make them easier to work with, but to be honest I'm also not sure if that's the best way to go about handling this. Any help or guidance would be appreciated!

    Code:
        Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
            OpenFileDialog1.Title = "Please Select a File"
            OpenFileDialog1.Filter = ("text files (*.txt) | *.txt")
            OpenFileDialog1.FileName = vbNullString           'Clear out previous filename
            startpath = My.Settings.loadPath
            OpenFileDialog1.InitialDirectory = startpath
            If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                selectedfile = OpenFileDialog1.FileName
    
                'Store the file path
                Dim sPath As String
                sPath = OpenFileDialog1.FileName 'get the path
                sPath = StrReverse(sPath) 'reverse the string
                sPath = Mid(sPath, InStr(sPath, "\"), Len(sPath)) 'extract from the first slash
                sPath = StrReverse(sPath) 'reverse it again
                My.Settings.loadPath = sPath
    
                'readinfile()
                Dim quoteArray As String() = File.ReadAllLines(selectedfile)
    
                Dim lst = quoteArray.ToList()
                Dim nullOrEmptyItemCount = lst.RemoveAll(Function(s) String.IsNullOrEmpty(s))
    
                Dim itemsPerBox As Integer
                itemsPerBox = lst.Count / nmThreads.Value
    
            Else
                Exit Sub
            End If
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Reading a Text File into Multiple RichTextBoxes

    Your business logic sounds like you need to do this:
    1. Get the text from the file
    2. Split the text up by each line
    3. Get the number of threads
    4. Divide the number of lines by the number of threads (round down?)
    5. Loop over the number of RichTextBox controls
    6. Skip the index of the currently iterated RichTextBox and take the result of 4
    7. Append those lines to the currently iterated RichTextBox


    Here is some untested code:
    Code:
    Dim lines = IO.File.ReadAllLines(selectedfile)
    Dim linesPerControl = lines.Length / nmThreads.Value
    For index = 0 To nmThreads.Value - 1
        Dim linesForControl = lines.Skip(index * linesPerControl).Take(linesPerControl).ToArray()
        Dim controlForLines = Controls.Find($"RichTextBox{index}")(0)
        controlForLines.Text = String.Join(Environment.NewLine, linesForControl)
    Next
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    Quote Originally Posted by dday9 View Post
    Your business logic sounds like you need to do this:
    1. Get the text from the file
    2. Split the text up by each line
    3. Get the number of threads
    4. Divide the number of lines by the number of threads (round down?)
    5. Loop over the number of RichTextBox controls
    6. Skip the index of the currently iterated RichTextBox and take the result of 4
    7. Append those lines to the currently iterated RichTextBox


    Here is some untested code:
    Code:
    Dim lines = IO.File.ReadAllLines(selectedfile)
    Dim linesPerControl = lines.Length / nmThreads.Value
    For index = 0 To nmThreads.Value - 1
        Dim linesForControl = lines.Skip(index * linesPerControl).Take(linesPerControl).ToArray()
        Dim controlForLines = Controls.Find($"RichTextBox{index}")(0)
        controlForLines.Text = String.Join(Environment.NewLine, linesForControl)
    Next
    That takes the lines sequentially, as they appear in the txt file, instead of the pattern the OP suggested...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Thanks for replying @dday9 and @.paul.

    @.paul. wouldn't the "skip" in his code prevent it from taking the lines sequentially?

    I wanted to test the code anyways to see how it puts them in, but getting an error.
    Do we want to searchAllChildren or not?


    That error is fixable by just setting a true/false:
    Code:
    Dim controlForLines = Controls.Find($"RichTextBox{index}", True)(0)
    Which do you recommend dday9?

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

    Re: Reading a Text File into Multiple RichTextBoxes

    In the Find, after the String, put either True or False if your RichTextBoxes are not in container controls

    For example of Skip and Take…

    One
    Two
    Three
    Four
    Five

    Skip(2).Take(2) would return…

    Three
    Four

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Thanks for replying .paul.! So, just an update after testing your quick code @dday9

    Setting the searchAllChildren to "false" gives an exception: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

    Setting the searchAllChildren to "true" does like @.paul. said - it puts the first 2 lines into each box, although it does seem to put the correct number of lines into the correct number of RichTextBoxes.

    For example I took 6 lines of data and selected 3 threads. This was the result:
    Code:
    RichTextBox1:
    line1 of data
    line2 of data
    
    RichTextBox2:
    line1 of data
    line2 of data
    
    RichTextBox3:
    line1 of data
    line2 of data

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    This is something similar to what i understood of your requirements...

    Code:
    Dim integers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    Dim arraysOfIntegers As New List(Of Integer())
    For x As Integer = 0 To (integers.Length \ 2) - 1
        arraysOfIntegers.Add(integers.Skip(x).Take(1).Concat(integers.Skip(integers.Length - (x + 1)).Take(1)).ToArray)
    Next
    'arraysOfIntegers equals...
            '{1,10}
            '{2,9}
            '{3,8}
            '{4,7}
            '{5,6}

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    It would be similar to that @.paul.

    However, the number of integers can change and wouldn't always to 10. With 5 selected in nmThreads.value, the arrangement would be:
    1,6
    2,7
    3,8
    4,9
    5,10

    Let's say there would only be 9 integers instead of 10 and you had 5 threads in the nmThreads.value..then I would want it to be like this:
    1,6
    2,7
    3,8
    4,9
    5

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    My snippet needs modifying slightly, but the great thing about Skip and Take is that they don’t cause IndexOutOfRange exceptions

    Try this…

    Code:
    Dim integers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    Dim arraysOfIntegers As New List(Of Integer())
    For x As Integer = 0 To (integers.Length \ 2) - 1
        arraysOfIntegers.Add(integers.Skip(x).Take(1).Concat(integers.Skip(x + integers.Length \ 2).Take(1)).ToArray)
    Next
    'arraysOfIntegers equals...
            '{1,6}
            '{2,7}
            '{3,8}
            '{4,9}
            '{5,10}

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Thank you for replying .paul.

    But, what if it's a String List instead of an Integer List?

    For example, if I try to convert your code over to a string list, like this:
    Code:
                Dim lines = IO.File.ReadAllLines(selectedfile)
                Dim arrayOfStrings As New List(Of String)()
                For x As Integer = 0 To (lines.Length \ 2) - 1
                    Dim linesForControl = arrayOfStrings.Add(lines.Skip(x).Take(1).Concat(lines.Skip(x + lines.Length \ 2).Take(1)).ToArray)
                    Dim controlForLines = Controls.Find($"RichTextBox{x}", True)(0)
                    controlForLines.Text = String.Join(Environment.NewLine, linesForControl)
                Next
    It doesn't like the "lines.Length" in this part: (x + lines.Length \ 2)
    Saying "Value of type 'String()' cannot be converted to 'String'."
    Last edited by digitaldrew; Nov 22nd, 2022 at 04:50 AM.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    I’m not guaranteeing it’ll work, but this’ll fix that error…

    Code:
    Dim linesForControl = lines.Skip(x).Take(1).Concat(lines.Skip(x + lines.Length \ 2).Take(1)).ToArray
    Also…

    Code:
    controlForLines.Lines = linesForControl

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Thanks .paul.

    Still messing around with it, but this does give an error:
    Code:
    controlForLines.Lines = linesForControl
    Lines is not a member of 'Control'

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    Code:
    Dim controlForLines = DirectCast(Controls.Find($"RichTextBox{x}", True)(0), RichTextBox)

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Once again, thanks for your speedy reply and help .paul.! So, a few things....

    The code you just sent
    Code:
    Dim controlForLines = DirectCast(Controls.Find($"RichTextBox{x}", True)(0), RichTextBox)
    returns an exception: System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.RichTextBox'.'

    However, this code seems to be working if my list is exactly 10 lines and I have exacty 5 threads selected:
    Code:
                For x As Integer = 0 To (lines.Length \ 2) - 1
                    Dim linesForControl = lines.Skip(x).Take(1).Concat(lines.Skip(x + lines.Length \ 2).Take(1)).ToArray
                    Dim controlForLines = Controls.Find($"RichTextBox{x}", True)(0)
                    controlForLines.Text = String.Join(Environment.NewLine, linesForControl)
                Next
    However, because of the loop (I'm guessing?), if I select something different, like 3 threads for my nmThreads.Value, it's still placing 2 per box in all 5 RichTextBoxes.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,076

    Re: Reading a Text File into Multiple RichTextBoxes

    As i said, they were immediate fixes, and i can't guarantee my snippet because your actual program is much more complicated than 5 RTB's and a 10 line array.
    Something along those lines but scaled for your purposes, is what i'd recommend...

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Nov 2012
    Posts
    150

    Re: Reading a Text File into Multiple RichTextBoxes

    Okay Paul, thanks. Both you and @dday9 were able to help me do more than just read in the file. I'm at least getting data into these now. Some of your code has fixed numbers in there, so I'm going to mess around with it some more and try to figure it out. Thank you again!

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Reading a Text File into Multiple RichTextBoxes

    See, this is why I should never sleep. I go to bed and then bam! There are a million replies.

    Haha!

    I trust you got it figured out with .paul.'s help, if not just post back here.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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