Vb.net Text file loop in Richtext box

July 1, 2010 at 15:13:34
Specs: Windows XP
I would be grateful with some help with reading a text file into a Richtext box. The code I have at present appends the first line of text as I want it but the rest of the lines of text do not alter. I need a loop to read to the end of file and display in Richtext box. the code i have at present is this:-

Dim FILE_NAME As String = "C:\Test.txt"
Dim sr As New System.IO.StreamReader(FILE_NAME)
RichTextBox1.Text = sr.ReadToEnd

Dim sb As New System.Text.StringBuilder(RichTextBox1.Text)
sb.Insert(5, " ")
sb.Insert(12, " ")
sb.Insert(18, " ")
sb.Insert(25, " ")
sb.Insert(29, " ")
sb.Insert(32, " ")
sb.Insert(37, " ")
sb.Insert(44, " ")
sb.Insert(45, " ")

RichTextBox2.Text = sb.ToString

sr.Close()


See More: Vb.net Text file loop in Richtext box

Report •

#1
July 4, 2010 at 09:45:29
It'd be helpful if you described what you wanted to do, and not just what didn't work.

Report •

#2
July 4, 2010 at 11:29:26
Hi Razor2.3

I have a text file which has lines of text at a fixed length I can create what I want with the code for the first line of text but I am struggling to sort out the code to read the original text file again to alter the rest of the text lines. At present all I can get is the 1st line altered how I want so it can be read more easily but the rest of the lines are in the original format.

The original text file
17915WHITE BLUE 1.900116A T123456111
72451BLACK ORANGE0.500208 B A123456123 'worst case
72455BLACK WHITE 2.703501 C123456124


Needs to look like below.
17915 WHITE BLUE 001.9 001 16 A T 123456 111
72451 BLACK ORANGE 000.5 002 08 B A 123456 123
72455 BLACK WHITE 002.7 035 01 C 123456 124

Then I can work on the converted file later but at present my main problem is as mentioned.


Report •

#3
July 4, 2010 at 14:02:50
There are a few ways to go about it; I would probably use StreamReader.ReadLine, apply my formatting, then take this line and add it via StringBuilder.AppendLine, and loop until StreamReader.EndOfStream was true

Report •

Related Solutions

#4
July 4, 2010 at 14:39:09
Hi Razor2.3,

Yes agree but my problem is constructing the loop code to achieve this.



Report •

#5
July 5, 2010 at 06:24:00
Dim sr As New System.IO.StreamReader("C:\test.txt")
Dim sb As New System.Text.StringBuilder
Do Until sr.EndOfSteam
  sb.AppendLine(FormatStr(sr.ReadLine)) 'Note: You'll have to write your own FormatStr(Of String) As String
Loop
RichTextBox2.Text = sb.ToString


Report •

#6
July 6, 2010 at 15:55:36
Hi Razor 2.3,

Thank you for the info I now have all the lines of text displayed and converted but I have a problem with the last line of text displaying first and the first line of text displaying last.
Also if I write the contents displayed in the Richtextbox to a file the text in the file shows as a continues line. I seem to be missing somthing but I am not sure what.
This is the code I have at present.

Dim FILE_NAME As String = "C:\Test.txt"
Dim sr As New System.IO.StreamReader(FILE_NAME)
Dim FormatStr As String = ""

Do Until sr.EndOfStream
Dim sb As New StringBuilder(sr.ReadLine & vbCr)
sb.AppendLine(FormatStr) 'Note: You'll have to write your own FormatStr(Of String) As String

sb = sb.Insert(5, " ")
sb = sb.Insert(12, " ")
sb = sb.Insert(19, " ")
sb = sb.Insert(25, " ")
sb = sb.Insert(29, " ")
sb = sb.Insert(32, " ")
sb = sb.Insert(37, " ")
sb = sb.Insert(44, " ")

FormatStr = sb.ToString
RichTextBox1.Text = FormatStr
Loop
sr.Close()

Dim objWriter As New System.IO.StreamWriter("C:\TextTest.txt")
objWriter.Write(RichTextBox1.Text)
objWriter.Close()


Report •

#7
July 6, 2010 at 16:49:37
Well, a FormatStr(Of String) As String, for one.

Report •

Ask Question