Getting the RichTextBox content into a string takes forever
Posted on 2009-05-11
I never thought I would be using experts-exchange ever (Mainly because this site is very slow and takes half a minute to load with my 3 Mbit cable), but for the first time in my life, Im stuck with a problem which I can not resolve on my own.
I have made a WordProcessor/text editor application in VB6 (with servicepack 4) based on the RichTextBox component. And this text editor is intended to handle large files > 5 Mb as well as smaller. But the RichTextBox seem to be handling text very slow by itself.
The RTB.loadfile is so slow that I made a sub to load the content:
Sub Load_File(FilNavn as string)
Dim Free As Long, FullText As String
Dim NewDocMode As Integer
FullText = ""
Free = FreeFile
Open FilNavn For Binary As #Free
Dim strBuff As String
strBuff = Space(LOF(Free))
Get #Free, , strBuff
FullText = strBuff
'# Check to find out if its plain text or formatted #'
NewDocMode = Proc_CheckFileType(FullText, FilNavn)
If (NewDocMode = 1) Then
RTB1.TextRTF = FullText
RTB1.Text = FullText
Using this Sub my Text Editor can load a 5 MB file in <2 seconds, which is acceptable.
Now to the big problem. All good Word Processors/Text Editors do have functions to count words, chars etc. and so does mine as well. But in order to count words/chars/etc I need to read the RTB content into a string and that takes almost forever with larger files! I also added reference to VB RegEx engine so I can do RegEx search in my Text Editor, but it also need the text content passed as string to work. And the RegEx search works fine, except the initializing where my function reads the RTB content into a string.
When I tried to debug my code to find out what was taking so long, I added some timing variables using GetTickCount API and the actual counting took approx 130 ticks, while reading the RTB.text content into a string took thousands of ticks.
For example, the content of a 2.8 MB file takes 9 seconds to read into my string variable, and for a 5.1 MB file it takes 30+ seconds. And that is not acceptable.
Here is the code who does read the RTB content into a string (in case you find some errors and bugs in it)
Function DoCount (sChar As String, RTB As RichTextBox) As Long
Dim StartTID As Long, SluttTID As Long
StartTID = GetTickCount
Dim LeteTekst As String, lCounter As Long
LeteTekst = RTB.Text
SluttTID = GetTickCount
Debug.Print ("Count: Result: " & lCounter & " - > It took " & (SluttTID - StartTID) & " ticks to read RTB content")
lCounter = ScanChar(sChar, LeteTekst) 'Used 130 ticks on a 4 MB file
MsgBox("There are " & lCounter & " -> " & sChar & " in the text")
DoCount = lCounter
The ScanChar procedure is working fast and as intended, so I will not post it here yet (unless you ask).
I even changed the RTB.Text to RTB.TextRTF to see if that was faster. But reading the formatted text is even slower than just getting the plain text out of the RTB, just as I thought.
Well, I hope you can help me with this, so my extra functions, char count, word count and regex search will work perfectly and not take forever.
Thanks in advance.