replace text in richtext box quickly - vb.net

i'm trying to load a text file to richtextbox using this codes
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText("path")
RichTextBox1.LoadFile("path", RichTextBoxStreamType.PlainText)
but both of them take time to load the file ,, the file size is around 400-1MB
so how to load it more quickly?
and with my code after loading the textfile i use this code
RichTextBox1.Text = Replace(RichTextBox1.Text, "text", "othertext")
but the problem is this take alot of time !!
How to do it quickly and save time :) , thanks!

You can cut the time almost in half by using an ordinary string variable instead of RichTextBox1.Text in the Replace function:
s = My.Computer.FileSystem.ReadAllText("path")
s = s.Replace("text", "othertext")
RichTextBox1.Text = s
You can combine these into one or two statements, but separating them allows you time each operation. The time-consuming part is accessing the RichTextBox control.

You could try reading it line-by-line:
Using Reader As New IO.StreamReader("<File Path>")
Do Until Reader.EndOfStream
Dim Line As String = Reader.ReadLine()
Line = Replace(Line, "text", "othertext")
RichTextBox1.AppendText(Line & Environment.NewLine)
Loop
End Using

Related

how to save text from richtext box to text document in vb.net

I'm taking value from richtextbox and i want to save it in new text document
I Want to save text of rich text box in text document as i write in rich text box but it stores like shown in image
My Code
Using Sw As StreamWriter = File.CreateText(path)
Dim number As String = RichTextBox1.Text
Sw.WriteLine(number)
End Using
Please help
The problem here is that both the TextBox and the RichTextBox control uses LF (Line Feed) as their newline format, but Windows's newline format is actually CR + LF (Carriage Return + Line Feed), which is what Notepad expects it to be.
The line breaks are there, Notepad just doesn't render them.
To fix this you can either replace all LFs with Environment.NewLine (which adapts to the current system) before saving:
Dim number As String = RichTextBox1.Text.Replace(vbLf, Environment.NewLine)
Sw.Write(Number)
...or you can save it line-by-line instead using the StreamWriter.WriteLine() method (which uses CR + LF for line breaks):
Using Sw As StreamWriter = File.CreateText(path)
For Each Line As String In RichTextBox1.Lines
Sw.WriteLine(Line)
Next
End Using
Read more:
Newline - Wikipedia (Line Feeds)
Carriage return - Wikipedia
After each Write, perform .Write(Environment.NewLine)

Reading a particular line of a text file in an array in VB.NET

I am trying to read specific lines from a text file in an array (e.g. line 16,25,34, and so on). Could you please let me know if it is possible and how that could be done?
Thanks in advance,
Pouya
Yes it is possible. Since this is not a code based will elaborate how to achieve that. This will depends on the size of your target file. If the size in not to large for your PC's memory then you can read the whole textfile while reading keep the count.
Then start when the file has been read to end to go through your lines using regex.
Check:
VB.NET Read Certain text in a text file
your solution is here:
http://www.dreamincode.net/forums/topic/56497-go-to-a-particular-line-in-a-text-file-using-vbnet/
How to read a specific line from a text file in VB
Ok, here's I've also quoted the code to help you from the second last like I provided above. I'm sure you know how to get data from an Array so instead of line you will add your array.
Public Function
ReadSpecifiedLine(ByVal line As
Integer) As String
'create a variable to
hold the contents of the file
Dim contents As String = String.Empty
'create a variable to
hold our line contents
Dim lineText As String =
String.Empty
' always use a
try...catch to deal
' with any exceptions
that may occur
Try
'Using lineByLine As New IO.StreamReader(_fileName)
Dim lineCount As Integer = 0
While Not lineByLine.EndOfStream
lineByLine.ReadLine ()
If lineCount = line Then
' you can replace the line variable above or use the And Or to match the lines from your array.
lineText = lineByLine.ReadLine()
End If
lineCount += 1
End While
End Using
Catch ex As FileNotFoundException
lineText = String.Empty
_returnMessage = ex.Message
Catch ex As Exception
' deal with any errors
_returnMessage = ex.Message
End Try
Return lineText
End Function
Hope this helps you.(Sorry having some problems in code formatting it some part maybe not formeted, or visible. If End Function is not visible please refer to the link. I've tried so many times to formet this but it not properly formeted, I'm using a Mobile phone.)

VB.Net Writing to Txt File

I'm trying to write the content of my textbox to a txt file.
My code works fine but my error is, when I open txt file I see
writeline1writeline2writeline3
instead of
writeline1
writeline2
writeline3
my code;
result As List(Of String) = New List(Of String)
convertedText.Lines = result.ToArray()
My.Computer.FileSystem.WriteAllText(mypath & "\convertedcontent.txt", convertedText.Text, False)
Writing to .csv and many other file types work fine but I don't know how to break lines for text file.
Thanks in advance
I would use System.IO.File.WriteAllLines:
Dim path = System.IO.Path.Combine(mypath, "convertedcontent.txt")
System.IO.File.WriteAllLines(path, result)
Otherwise you need to append Environment.NewLine to each line, you can use String.Join:
System.IO.File.WriteAllText(path, String.Join(Environment.NewLine, result))
You need to add & vbCrLf to your strings (each line)
Not sure where you are getting your strings from.. but you will have to add the carrier return/Line Feed character to those strings, one at the end of every string.
Might just even loop through your array and add them there?
P.S. Some of the comments have quicker ways of getting there, but this is probably what happens behind the scenes...
for i = 0 to convertedText.Lines.count -1
convertedText.Lines(i) += vbCrLf
next

rich textbox combines all lines into one when saved as txt file?

I have a rich textbox in vb.net, which contains several lines of text however when I try and save the text to a .txt file all the lines are combined into one???
How can I overcome this?
what i did try was:
Dim MYLINES As Object
For Each MYLINES In RichTextBox1.Text
objWriter.WriteLine(MYLINES.ToString & Environment.NewLine)
Next
objWriter.Close()
however this simply placed every single character on a difrent line...
Save it as a .RTF file (instead of .TXT), wich can process the line breaks directly. Thats an option.
Or you can wirte the lines individually:
Dim sw As New System.IO.StreamWriter(sFileName)
For Each sLine as String in TextBox1.Lines
sw.WriteLine(sLine)
Next
sw.Close()
The method WriteLine already adds a line break at the end. It more or less the same as:
.Write(sLine & Enviroment.NewLine())

Can I stop my textbox from crashing when loading in too much data into it?

Not sure if this is something really simple that I haven't noticed. Or just a simple try/catch (I guess most likely).
I have a textbox on a form. It takes in data from another text file which is very large (lets assume 1Mb) and in a lot of cases it can take it but in some cases the program crashes when loading the file.
How can I handle this?
You could try to read the file size of the text file before loading it into your text box. If the length of the file is greater than the capacity of your text box, you can prevent the text box from being filled.
You can use a function like File.ReadAllLines to determine the size of the text file.
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
For Each fileSystemInfo As System.IO.FileSystemInfo In _
SourceDirectoryInfo.GetFileSystemInfos
Dim FileText As String = System.IO.File.ReadAllText _
(AppDataLocation & fileSystemInfo.Name)
Next
Alternately, you can use FileInfo.Length as seen in this example here.
Dim file As New FileInfo("file.txt")
Dim sizeInBytes As Long = file.Length