views:

40

answers:

1

Hi. I have a WinForms program where, whenever you change your selection, the RichTextBox needs to change the colour of certain other text. In order to do this, it has to select that text, and therefore I lose my current selection.

I can save and load the SelectionStart and SelectionLength properties, but I can't keep the "selection direction": if the user was highlighting forwards or backwards from the cursor.

Any ideas about how I can either save the selection direction, or colour the text without having to change the selection?

+1  A: 

Yuck, ugly problem. No, EM_SETPARAFORMAT can only work on the current selection. And EM_EXSETSEL always puts the caret at the end of the selection. You could detect the selection direction by observing the change in SelectionStart but you can't get the caret in the right spot. An edit control has the same problem.

It isn't normally a problem because re-coloring happens only when the user modifies text, not when she's selecting text. The only workaround I can think of is restoring the selection by injecting keystrokes. That's fugly.

Hans Passant
I wonder what would happen if you used EM_EXSETSEL and had cpMax as a negative number.
Miguel