Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello All!

So, just like a lot of others, I thought I'd try my hand at colorizing keywords. Not that I need it for anything serious... just figured it would be fun to try... and so far it has been fun!

Anyways, what I have works pretty good. However, I'm wondering if there is any way to speed things up a bit.

Keep in mind... I am a beginner at this so I may be doing something wacko! If I did, please let me know.

So, what I have is an inherited richtextbox with the following code:
Private Sub ColorizePastedText()
        Dim CursorPosition As Integer = Me.SelectionStart
        Dim RegXMatches As MatchCollection
        Dim RegXMatch As Match
        Dim tmpRTB As New RichTextBox
        'LastRTBLength is the Textlength before the paste. RTB TextLength stored on key down event
        Dim PasteLength As Integer = Me.TextLength - LastRTBLength
        'Get rid of the RTB flickering
        LockWindowUpdate(Me.Handle.ToInt32)
        tmpRTB.Rtf = Me.Rtf
        tmpRTB.SelectAll()
        tmpRTB.SelectionFont = Me.Font
        'Find keywords then colorize them
        For Each KeyWord In KeywordList
            RegXMatches = System.Text.RegularExpressions.Regex.Matches(tmpRTB.Text, "\b" & KWord & "\b")
            For Each RegXMatch In RegXMatches
                If RegXMatch.Index >= LastCurPos AndAlso RegXMatch.Index < LastCurPos + PasteLength Then 'Colorize only what was just pasted
                    tmpRTB.Select(RegXMatch.Index, RegXMatch.Length)
                    tmpRTB.SelectionColor = KeywordColor
                End If
            Next
        Next
        Me.Rtf = tmpRTB.Rtf

        'Put the cursor back where it started from
        Me.SelectionStart = CursorPosition
        'Set the RichTextBox's color back to normal
        Me.SelectionColor = Me.ForeColor
        'Unlock RichTextBox
        LockWindowUpdate(0)
    End Sub


The idea is that it will colorize keywords that are newly pasted and ignore anything that was already in the RTB.
If I copy a hundred or so lines of text then paste it into the RTB it processes the text within a reasonable amount of time. However, if I past the same text again it takes a bit longer to process. The process time continues to increase with each additional paste.
I know I lose a tiny bit of time in transferring the rtf data as it is larger, but the real loss is in the "For" statement. If I throw in
"tmpRTB.Select()" right after "tmpRTB.SelectionColor = KeywordColor" It will select what was pasted... telling me that it is working within only the pasted area.

I'm sure it's something simple... I'm just not seeing it.! :)

Any ideas will be greatly appreciated!

Thanks!
Posted

1 solution

Well, because of the increasing amount of time needed I suspect the following.

You are working on the actual RTF in the RTF component on your form. So each time you paste it into it, the RTF is becoming larger and harder to process. This is because each style you apply to the text will add new data and makes it necessary to copy it in case the current allocated memory runs out. Because you already concatenated it to the complete text this means that all of the previous text has to be copied as well to the new allocated memory.

A simple option to avoid this process of getting slower and slower is to simply get the pasted text before it is actually pasted into the rtf control:
http://www.vbforums.com/archive/index.php/t-374169.html[^]

Then is it is text you create (or use an already standby) RichTextBox component to handle that text and process the highlighting in there. When you're done you paste the rtf with all highlighting into the total rtf.

You could also use this component for adding the styles to the text instead of using a visual component. But since you are a beginner I don't know how easy you could use it, but you can always have a look.

NRTFTree - A class library for RTF processing in C#[^]

Good luck!
 
Share this answer
 
Comments
junrall 17-Dec-10 1:35am    
Thanks for the feed back!
I too was thinking that it might be best\easier to process the text before it was actually pasted.
Though, I did take a look at Salvador's NRtfTree... I understand some of and should be able to get some ideas there. LOL... not only do I have to decipher his code, I'll have to learn a little Spanish too! Ah well, the learning process is never ending!
E.F. Nijboer 17-Dec-10 4:31am    
I think it would also help to simply use another RichTextBox to do syntax highlighting for the pasted text.
junrall 20-Dec-10 3:51am    
Well... capturing and coloring the pasted text before it is actually pasted definitely sped things up! It's a good workaround... for now anyways!

Thank you Nijboer!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900