Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of a rich text box when it is disabled?

Whenever I set the RichTextBox.Enabled property to false, its background color is automatically set to gray as it is set to the color in system color which is set in the control panel. How can I change its color to black even if I set it to disabled?

like image 445
Badr Avatar asked Dec 22 '09 12:12

Badr


People also ask

How do I change the color of a disabled text box?

I think what you really want to do is enable the TextBox and set the ReadOnly property to true . It's a bit tricky to change the color of the text in a disabled TextBox . I think you'd probably have to subclass and override the OnPaint event. ReadOnly though should give you the same result as !


1 Answers

See: How to change the font color of a disabled TextBox?

[Edit - code example added]

richTextBox.TabStop = false;
richTextBox.ReadOnly = true;
richTextBox.BackColor = Color.DimGray;
richTextBox.Cursor = Cursors.Arrow;
richTextBox.Enter += richTextBox_Enter;

private void richTextBox_Enter(object sender, EventArgs e)
{
    // you need to set the focus somewhere else. Eg a label.
    SomeOtherControl.Focus();
}

or as en extension method (I realized you don't have to put it in readonly since the Enter event catches any input):

public static class MyExtensions
{
    public static void Disable( this Control control, Control focusTarget )
    {
        control.TabStop = false;
        control.BackColor = Color.DimGray;
        control.Cursor = Cursors.Arrow;
        control.Enter += delegate { focusTarget.Focus(); };
    }
}
like image 147
Mikael Svenson Avatar answered Sep 28 '22 19:09

Mikael Svenson