Getting the RichTextBox content into a string takes forever


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
    Close #Free

    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
    End If
End Sub

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
End Function

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.
Who is Participating?
FyrstenAuthor Commented:
Seeing that my question has been open for 8 hours with no replies is quite weird, especially as Experts-Exchange was bragging about how fast you could get a reply here and that the average response time was 49 minutes...

It could be that I gave you too much information, and that I should have done like many other people do here. Just ask a question without giving any information, assuming that you so called "experts" are psychics and mindreaders.

But it any event, I thought more about my problem and realized that VB controls in general seem to have slow methods and properties. And since my application are utilizing a lot of API calls in the order to get stuff done (Like using BitBlt API instead of the very slow PictureBox.PaintPicture method) , why not find one to retrieve the text from the RTB. And after checking some API documentation, I then decided to try the WM_GETTEXT with SendMessage API.

And it's amazing!! This function actually reads a 5 MB file from RTB to a string in less than 2 seconds! Thats much better than waiting 30+ seconds while the Computer nearly hangs in the attempt of getting the text out of a RichTextBox

I'll provide the solution to my own question for future readers (if any)
Private Const WM_GETTEXT = &HD
Public Function GetRTBText(RTB As RichTextBox) As String
    Dim lLength As Long
    Dim lResult As Long
    Dim strtmp As String
    If (RTB Is Nothing) Then GetRTBText = "Error! Invalid Object reference Arghhhh!!!!": Exit Function
    lLength = SendMessage(RTB.hWnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)
    strtmp = Space(lLength)
    lResult = SendMessage(RTB.hWnd, WM_GETTEXT, ByVal (lLength + 1), ByVal strtmp)
    GetRTBText = strtmp
End Function

Open in new window

I think your problem is symptomatic of hardware issues or service pack issues..
I tried a 7 meg  NormalText = RTB.Text which only took 1 second..
With VB6, you should be on service pack 6 by now, but I don't think that is the sole problem.
FyrstenAuthor Commented:
You are probably right BrianVSoft

But I downloaded and installed servicepack 6, and the RTB.text property are still very slow.

Perhaps it has something to do with me using a 9 year old Pentium III 666 Mhz computer with 512 Mb Ram. But there is a reason why I do this. Because when my application is able to run fast on a darn old computer, then it will probably work fine on a newer computer. And it runs fast now with the use of WM_GETTEXT and SendMessage API.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.