views:

363

answers:

3

While I do know the above goal is rather challenging to achieve, I know it can be done.

What the one I made does:

It compares each word to a word from the list, if its a match, it will physically select the text and change the color, and finally it will return the carat to the position it was in when it highlighted.

Issues with this:

  • Flickering. It always flickers. I managed to reduce the flicker by making the thread sleep for 50 milliseconds, but I could not get rid of it entirely (it didn't noticeably slow down typing either). Now, another thing that I tried was using a second RichTextBox to have the highlighting occur to that, but that made no obvious differences at all.

  • Scrolling. It will scroll the RichTextBox if the amount of text is big enough to cause scrollbars to appear.

  • Deleting text. If you delete part of a word which has been highlighted, itll retain the color formatting even if the word is now changed. Itll also physically select the entire word, which baffles me endlessly.

  • Closing the window. Since the highlight subroutine is called whenever the RichTextBox's TextChanged event is called, apparently it is fired when the window closes? However, since the window is closing, it slows down the entire process, so with a larger file it may take 5-10 seconds to go through and highlight each keyword.

How can I solve each of these issues? I do not want to use someone else's component, I specifically want to use my own.

Thanks for the help, I have spent several hours on this and so far I am pleased with the results.

Some ideas I have had:

  • Physically edit the RTF instead of using a built in method for changing the text color, this would get rid of all my issues. Assuming a keyword was "The" (for example), what would the rtf be to make it turn blue?

  • Don't use syntax highlighting (last resort here)

My end goal: A syntax highlighter for a RichTextBox which works as well as the one in Visual Studio.

EDIT: Is there another component which would be better for this than a RichTextBox?

+1  A: 

I just saw this page which deals with using RichTextBox for syntax highlighting but in C#. The thing, however, is that the second post talks about the flickering you have, I'm not sure if their solution would help you. And it seems like they are re-checking the highlights every time the text changes, which should solve your problem where the word remains highlighted even if changed.

There is also a very thorough article on CodeProject regarding Real Time Syntax Highlighting with Visual Basic which is definitely worth reading.

As for components, this question covers that.

Sorry if I misunderstood you.

Jorge Israel Peña
Saw it, but I couldnt get the fix for the highlights working.
Cyclone
As in, they didn't have any effect? Or they would generate errors and not compile?
Jorge Israel Peña
No visible effect :(
Cyclone
A: 

You might want to take a look at the source code for Sharp Develop, their editor is fully functional and highlights without flicker.

Also if you can get hold of a copy of their book called "Dissecting a C# Application - Inside Sharp Develop", that has a few chapters dedicated to to the editor - data structures, highlighting, ui

Pondidum
Too much code to dig through to find what I need, but thanks anyway!
Cyclone
+1  A: 

I don't know if you're in Windows.Forms or WPF here, but if it is Windows.Forms, I wrote a blog post about this issue some years ago, and it may apply for you. I did this for a chat-program and it worked well for me. Blog post is here.

Johan Danforth
Winforms, thanks! Ill check out your post.
Cyclone