Solved

Getting the RichTextBox content into a string takes forever

Posted on 2009-05-11
3
678 Views
Last Modified: 2012-06-21
Hi

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
    Else
        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.
0
Comment
Question by:Fyrsten
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
Fyrsten earned 0 total points
ID: 24360605
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

Private Const WM_GETTEXTLENGTH = &HE
 

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

0
 
LVL 5

Assisted Solution

by:BrianVSoft
BrianVSoft earned 50 total points
ID: 24370349
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.
0
 
LVL 1

Author Comment

by:Fyrsten
ID: 24377226
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.

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now