After having done some research, I believe I am asking the same question as Remove richtextbox SelectionBackColor. I have encountered the same issue, but I believe the answers in that thread were insufficient as the question was not clearly explained. Please see below:
In a RichTextBox
, how do I remove a custom BackColor
from some, but not all, of the text (SelectionBackColor
) so that it assumes the BackColor
of the control even if that BackColor
changes in the future?
I have a method that highlights some text and changes its BackColor
using SelectionBackColor
. I have another method that changes the BackColor
of the entire control. These events can happen independently.
If I want to "remove" some SelectionBackColor
, I can try to set the SelectionBackColor
to Color.Transparent
, but it ends up being White. That is fine, temporarily, if my RichTextBox
's current BackColor
is White. If I set SelectionBackColor
to the current BackColor
, it is fine temporarily, until that BackColor changes from another method.
After the RichTextBox.BackColor
has changed, any places that were previously highlighted use White or the previous BackColor
, instead of assuming the new color like text that had not previously highlighted.
I have tried deleting and replacing the text, but that negates the ability to retain any other custom formatting of that text, to my knowledge. Setting SelectionBackColor
to null does not work.
One can easily see what I am talking about using the code below:
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
this.BackColor = Color.Gray;
if (SelectionLength > 0)
{
SelectionBackColor = Color.Yellow;
}
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
this.ResetBackColor();
if (SelectionLength > 0)
{
// The goal of this line is to "remove" the yellow.
// By assigning it any value, it seems to have lost
// the ability to use the control's BackColor normally.
SelectionBackColor = this.BackColor;// or Color.Transparent
}
}
Type some text into a custom RichTextBox
object with the code above, highlight a small portion of it, then make the box lose focus. You will see the highlighted text in yellow. Then, make the box gain focus. The yellow background will go away, as expected. However, if you move your caret elsewhere in the text and make the control lose focus again, you will see the previously-highlighted text does not assume the gray background color.
I did:
and it worked fine.
This is interesting. It appears that (on Windows 7/.Net 3.5 where I tested and perhaps elsewhere)
System.Windows.Forms.RichTextBox.SelectionBackColor
may have a bug clearing the selection back color. The source code does:This does not, as you have noticed, actually clear the back color when set to
richTextBox.BackColor
. Nor does it clear the color when set toRichTextBox.DefaultBackColor
, which simply sets the selection back color to the default grey control color. It looks as though the source code is trying to clear the selection back color when set toColor.Empty
-- but on my machine at least, it does nothing.But if I create an extension method that also sets
cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR;
for an empty color before sending the message, thenSetSelectionBackColor(Color.Empty)
now works!Full method, with constants and classes adapted from here and here and here and here and here: