1

In CSS we're able to edit the text selection's colors by using the ::selection pseudo-tag. Is it possible, to overwrite the default background color of the selection just for one control, e.g. a RichTextBox? I know, that there is no way to change the default behaviour by something like CSS, but at least overwrite it for this control might be possible.

I already googled for about an hour now, but I only found snippets of syntax highlighting. I want the text to be e.g. yellow instead the typical Windows blue.

EDIT

Like in this fiddle: http://jsfiddle.net/W99Gt/

2
  • 1
    Are you using WPF or WinForms? Feb 12, 2014 at 19:39
  • I'm currently using WinForms, because it was faster to integrate the UI into my project. But I think, this problem will be the same for WPF
    – michaeln
    Feb 12, 2014 at 20:04

2 Answers 2

2

In WPF you can accomplish this as follows:

myRichTextBox.SelectionBrush = System.Windows.Media.Brushes.Yellow; // WPF
myRichTextBox.IsInactiveSelectionHighlightEnabled = true;

Unfortunately, the desired behavior is not possible in Windows Forms (details here). The workaround would be to use a WPF RichTextBox in the Windows Form through ElementHost.

References:
TextBoxBase.SelectionBrush Property (WPF)
TextBoxBase.IsInactiveSelectionHighlightEnabled Property (WPF)


EDIT:
Removed the WinForms solution, because SelectionBackColor does not provide the desired behavior.

12
  • That's pretty much the same as stackoverflow.com/a/13220947/2058898 , but thanks anyway for giving me the corresponding WPF example
    – michaeln
    Feb 12, 2014 at 19:58
  • I fixed the code. It was supposed to be SelectionBackColor for the WinForms solution. Feb 12, 2014 at 20:08
  • I think the word Selection means something completely different in this case. It's something like the selected words which you can manipulate by editing this attribute. I tried this one before (and right now) and it changed the backgroundcolor of the selected text. After I defocused the selection, the background of the previously selected text had changed.
    – michaeln
    Feb 12, 2014 at 20:17
  • So you want the select text background color to remain the same even if the RichTextBox looses focus, right? Feb 12, 2014 at 20:29
  • 1
    I understand what you mean now. The WPF code above will do that. The WinForms code will need more work... hold on. Feb 12, 2014 at 20:49
1

There is a property RichTextBox.SelectionColor which should do the work. Quoting MSDN

A Color that represents the color to apply to the current text selection

1
  • Thank you for your fast answer. This won't solve my problem, because it sets the foreground color of my selected text. This is actually the same snippet I use for syntax highlighting editor.Select(start, end); editor.SelectionColor = color;.
    – michaeln
    Feb 12, 2014 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.