ryan2014
ryan2014

Reputation: 19

replace text in richtext box quickly

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!

Upvotes: 0

Views: 179

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18310

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

Upvotes: 1

xpda
xpda

Reputation: 15813

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.

Upvotes: 1

Related Questions