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/

michaeln
  • 1,032
  • 17
  • 33

2 Answers2

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.

Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
  • That's pretty much the same as http://stackoverflow.com/a/13220947/2058898 , but thanks anyway for giving me the corresponding WPF example – michaeln Feb 12 '14 at 19:58
  • I fixed the code. It was supposed to be `SelectionBackColor` for the WinForms solution. – Wagner DosAnjos Feb 12 '14 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 '14 at 20:17
  • So you want the select text background color to remain the same even if the RichTextBox looses focus, right? – Wagner DosAnjos Feb 12 '14 at 20:29
  • Do you know, what `::selection` does in CSS? I want to achieve this effect. I'll create a jsfiddle and edit it to my question – michaeln Feb 12 '14 at 20:30
  • Please try with `HideSelection = false`. This will keep the selected text background visible even when the control looses focus. – Wagner DosAnjos Feb 12 '14 at 20:38
  • I don't think we're talking about the same problem. My intention is, to edit the background color of the current selection. If I deselect (defocus the selection), the background should stay, what it was before. The only thing I want to change is the background color of the selection, like in the jsfiddle I posted in my question. – michaeln Feb 12 '14 at 20:43
  • 1
    I understand what you mean now. The WPF code above will do that. The WinForms code will need more work... hold on. – Wagner DosAnjos Feb 12 '14 at 20:49
  • Humm... It seems it cannot be done with WinForms http://social.msdn.microsoft.com/Forums/windows/en-US/692bb113-737d-462c-adef-dffc465a1282/richtextbox-selection-color?forum=winforms – Wagner DosAnjos Feb 12 '14 at 21:17
  • It seems the only workaround would be to use a WPF RichTextBox in your Windows Form via [ElementHost](http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost(v=vs.110).aspx). – Wagner DosAnjos Feb 12 '14 at 21:22
  • Then I think i might have to switch to WPF. Thanks for helping me out, although it wasn't possible. If you edit your answer to contain this, I'll mark it as answer, so other users can see this. – michaeln Feb 12 '14 at 21:23
  • Edits applied. Thanks. – Wagner DosAnjos Feb 12 '14 at 21:32
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

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • 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 '14 at 19:57