How to change the background color of a rich text box when it is disabled?
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(); };
}
}
I've just found a great way of doing that. It should work with any Control:
public class DisabledRichTextBox : System.Windows.Forms.RichTextBox
{
// See: http://wiki.winehq.org/List_Of_Windows_Messages
private const int WM_SETFOCUS = 0x07;
private const int WM_ENABLE = 0x0A;
private const int WM_SETCURSOR = 0x20;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (!(m.Msg == WM_SETFOCUS || m.Msg == WM_ENABLE || m.Msg == WM_SETCURSOR))
base.WndProc(ref m);
}
}
You can safely set Enabled = true and ReadOnly = false, and it will act like a label, preventing focus, user input, cursor change, without being actually disabled.
See if it works for you. Greetings