Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Editors
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 3-Jan-13 9:10am
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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? Wink | ;-)
 
[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
  Permalink  
v3
Comments
Espen Harlinn at 3-Jan-13 18:48pm
   
Congratulations on passing 500K, Sergey :-D
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid