-
Nov 21st, 2022, 06:10 AM
#1
Thread Starter
Addicted Member
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
-
Nov 21st, 2022, 06:02 PM
#2
Re: Reading a Text File into Multiple RichTextBoxes
Your business logic sounds like you need to do this:
- Get the text from the file
- Split the text up by each line
- Get the number of threads
- Divide the number of lines by the number of threads (round down?)
- Loop over the number of RichTextBox controls
- Skip the index of the currently iterated RichTextBox and take the result of 4
- 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
-
Nov 21st, 2022, 06:29 PM
#3
Re: Reading a Text File into Multiple RichTextBoxes
Originally Posted by dday9
Your business logic sounds like you need to do this:
- Get the text from the file
- Split the text up by each line
- Get the number of threads
- Divide the number of lines by the number of threads (round down?)
- Loop over the number of RichTextBox controls
- Skip the index of the currently iterated RichTextBox and take the result of 4
- 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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 21st, 2022, 11:51 PM
#4
Thread Starter
Addicted Member
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?
-
Nov 22nd, 2022, 12:03 AM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 12:26 AM
#6
Thread Starter
Addicted Member
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
-
Nov 22nd, 2022, 12:46 AM
#7
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}
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 01:17 AM
#8
Thread Starter
Addicted Member
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
-
Nov 22nd, 2022, 01:27 AM
#9
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}
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 04:47 AM
#10
Thread Starter
Addicted Member
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.
-
Nov 22nd, 2022, 05:08 AM
#11
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 05:15 AM
#12
Thread Starter
Addicted Member
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'
-
Nov 22nd, 2022, 05:20 AM
#13
Re: Reading a Text File into Multiple RichTextBoxes
Code:
Dim controlForLines = DirectCast(Controls.Find($"RichTextBox{x}", True)(0), RichTextBox)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 05:35 AM
#14
Thread Starter
Addicted Member
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.
-
Nov 22nd, 2022, 06:00 AM
#15
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 22nd, 2022, 07:01 AM
#16
Thread Starter
Addicted Member
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!
-
Nov 22nd, 2022, 09:48 AM
#17
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|