views:

682

answers:

5

HI,

I have the following problem- the following text is in a rich text box .

The world is [[wonderful]] today .

If the user provides two brackets before and afer a word, as in the case of wonderful , the word in brackets, in this case, wonderful shall change to a link, ( with a green colour ) .

I am having problems in getting the sequence of the keystrokes, ie. how do I know that the user has entered [[ , so I can start parsing the rest of the text which follows it .

I can get it by handlng KeyDown, event, and a list , but it does not look to be elegant at all.

Please let me know what should be a proper way.

Thanks, Sujay

+5  A: 

You have two approaches that I can think of off-hand.

One is, as you suggest, maintain the current state with a list—was this key a bracket? was the last key a bracket?—and update on the fly.

The other approach would be to simply handle the TextChanged event and re-scan the text for the [[text-here]] pattern and update as appropriate.

The first requires more bookkeeping but will be much faster for longer text. The second approach is easier and can probably be done with a decent regex, but it will get slower as your text gets longer. If you know you have some upper limit, like 256 characters, then you're probably fine. But if you're expecting novels, probably not such a great idea.

jeffamaphone
If links cannot be multi-line (often the case) you don't need to scan the entire string if you maintain a copy of the string stored by lines. Then each line can be parsed seperately and it doesn't scale by total string size.
Ron Warholic
I have already done the KeyDown event and captured the brackets, but I dont think thats decent programmng Isnt there any approach with design patterns that can solve this problem .E.g I am thinking in this lines .The [[wonderful]] text is an object . When I encounter the braces, I delegate it to a function, say SetFormatStyle(text), which does the needful.
Sujay Ghosh
A: 

Keeping a list will be rather complex I think. What if the user types a '[' character, clicks somewhere else in the text and then types a '[' character again. The user has then typed two consecutive '[' characters but in completely different parts of the text. Also, you may want to be able to handle text inserted from the clipboard as well.

I think the safest way is to analyze the full text and do what should be done from that context, using RegEx or some other technique.

Fredrik Mörk
+1  A: 

I would try handling the KeyDown, and checking for the closing bracket instead "]". Once you receive one, you could check the last character in your text box for the second ], and if it's there, just replace out the last few characters.

This eliminates the need for maintaining state (ie: the list). As soon as the second ] was typed, the block would change to a link instantly.

Reed Copsey
+2  A: 

I would recommend Google'ing: "richtextbox syntax highlighter", there are so many people that have done this, and there is a lot behind the scenes to make it work.

I dare myself to say, that EVERY SINGLE simple solution have major drawbacks. Proper way would be to use some control that already does this "syntax highlighting" and extending it to your syntax. It is also most likely the easiest way.

You can search free .net controls in Codeplex. link

Margus
A: 

(Sorry, don't have enough reputation to add comments yet, so have to add a new answer). As suggested by jeffamaphone I'd handle the TextChanged event and rescan the text each time - but to keep the cost constant, just scan a few characters ahead of the current cursor position instead of reading the entire text.

Trying to intercept the keystrokes and maintain an internal state is a bad approach - it is very easy for your idea of what has happened to get out of sync with the control you are monitoring and cause weird problems. (and how do you handle clicks? Alt-tab? Pastes? arrow keys? Other applicatiosn grabbing focus? Too many special cases to worry about...)

Jason Williams