Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello guys
I would like to make a code editor that highlight my keywords while writing.
should I make a method that scan the whole richtextbox every second (using a timer) -(and that is bad bad for performance ) or is there another methode ?
thanks a lot in advance and sorry for my lack of experience :(
Posted
Comments
Sergey Alexandrovich Kryukov 3-Jan-13 15:53pm    
Is is System.Windows.Forms.RichTextBox or something else?
When you ask questions of UI, you should always tag the UI library you use. For example: "Forms", "WPF". Or provide full type names.
—SA

1 solution

Of course you are right: polling by timer… bad idea. Of course, you should not do it.

Here is my pitch: where is your logic? Follow me: if you are already ready to handle some events to update the properties, why not looking for some other events, more suitable ones. But what events? Apparently, only those which change anything essentially. The main problem with polling is: nothing has changed to the control content, but you are still polling for possible change, which is bad. And what events could possibly change the content of this control? Of course, input events. If the user does not input anything (including deletion of some text/objects), nothing is changed, nothing to look for.

Are you getting the idea? ;-)

[EDIT]

Let's make a next step. Can we choose just one event, the only one worth handling for your purposes? This is the event SelectionChanged. Even better, you can override the virtual method OnSelectionChanged:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionchanged.aspx[^],
http://www.codeproject.com/script/Reputation/List.aspx?mid=2291164[^].

You cannot possible change the content of the control without changing the selection; even of you never select anything, if you move the caret, it means the property SelectionStart is changes. This is just the selection, I tell you.

Why this event is so good? You are not risking to fall into an infinite recursion. If you do you highlighting, you don't change selection.

But if you have some algorithm changing selection, you should switch off the handling inside handling (use some flag which should be you control's property), change selection inside the handler, and restore processing flag after you are done. In other words, make it non-reentrable.

Problem solved.

Good luck,

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 3-Jan-13 18:48pm    
Congratulations on passing 500K, Sergey :-D
Sergey Alexandrovich Kryukov 3-Jan-13 19:00pm    
Thank you very much, Espen.

Interestingly, I noticed exactly 500,000 on January 1st, 2013.
By the way, some half of those points or so thanks to you, don't you think so?

But what's the ado? :-) Next round value will be only 524,288 = 1M/2...

—SA

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