2
votes

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?

5

5 Answers

2
votes

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.

1
votes

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.

0
votes

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

0
votes

Scintilla, it is free and open source. It supports a lot of languages and is easy to use. It is a real time syntax highlighter add-on for Visual Studio and requires little to no code to get working, it also has 600 tools to make a pre IDE so it is a good add-on and completely editable.

0
votes

if richtextbox have no focus, it does not flicker. I solved it temporary by removing focus and then setting it again if it was focused before

Dim lWasIFocused As Boolean = Me.Focused
' Removes flicker when i am not focused
Parent.Focus()

' enter your code here

If lWasIFocused Then Me.Focus(