views:

69

answers:

0

Ok, this will be a long one:

I started working on a simple OpenGL-SL IDE and I started the implementation of a text highlighter. Until now everything works fine: I have a class with some methods that check for different keywords on a string and change a RichTextBox's text color.

The main method which handles all of the text painting is called everytime the text changes, i.e. inside the OnTextChange. The function receives a reference to the RichTextBox control, a start position on the string (textBox.SelectionStart - 1) and the length of the string it has to analyze(on normal keyboard input, that's always 1).

Everything nice and sound until a paste occurs. I'll let you guess why!

So, so far I have this: if pasting from the keyboard (with ctrl+v), I figure it would be easy to implement something like this: - make a child class of the RichTextBox, with this occasion solve the autoWordSelection bug and include the class which does the highlighting in the new derived richTextBox - override the OnKeyDown event and, if ctrl+v pressed, set SuppressKeyPress to true and call another function (let's call it "Paste2" for now) to get the data from the clipboard, insert it into the textbox's text and call the method which highlights the keywords with the proper parameters (start position & length)

After that, for any other type of pasting (right click on text and select "paste" from a dropbox for example), I would just make sure I call Paste2 instead of the default inherited Paste method.

So far I think the only drawbacks of this implementation are: 1. the highlighting function gets called twice when pasting (once with the proper parameters and then on the OnTextChange event) 2. instead of overriding the paste method I just bypass it

The questions are:

How "ugly" is this implementation? Would this implementation rise other problems that I'm not aware of? Is there any better solution?

Any input will be greatly appreciated!

PS: yes, I DO want to reinvent the wheel. I know that tons of solutions are out there, but I want to do this one from scratch, as an exercise.

PPS: I don't consider parsing the whole string in the .Text property of the richTextBox a solution