Click here to Skip to main content
12,760,849 members (30,877 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET editing
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 10:10am
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.

1 solution

Rate this: bad
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? ;-)


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:[^],[^].

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,

Espen Harlinn 3-Jan-13 18:48pm
Congratulations on passing 500K, Sergey :-D
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...


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

    Print Answers RSS
Top Experts
Last 24hrsThis month

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100