17

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?

Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
Badr
  • 10,384
  • 15
  • 70
  • 104

6 Answers6

16

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(); };
    }
}
Community
  • 1
  • 1
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • its not the thing i asked for! – Badr Dec 22 '09 at 13:26
  • 3
    @moon: Yes it is, you have to set the control to readonly and set your colors manually instead of disabling it. Disabling a control will use the OS settings for how it's rendered. – Mikael Svenson Dec 22 '09 at 17:15
  • mr. Mikael Svenson if i set it readonly it displays the cursor and one can also copy the data i want to give it a look of on text but the other options should not be there, i use lable but there are problems with resizing in dyanmically if there are some options to hide cursors and no copy paste option then it works i have control the copy paste option by dispose the cursor but the ibeam is still blinking how to handle it if it can be handled my problem will be solved wating for reply! – Badr Dec 23 '09 at 04:57
  • Added code example on how you can get it to work so that the cursor won't blink. – Mikael Svenson Dec 23 '09 at 11:07
  • Just a note for future readers that you still have the carot visible in the textbox and it is dependent on setting the background the same color as the carot to hide it. If you have changed the colors you will get the odd artifact of the carot following the mouse pointer around. – denver Jun 11 '20 at 15:41
8

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

edoedoedo
  • 1,469
  • 2
  • 21
  • 32
4

Set the backcolor property to your desired color and then set the richtextbox to readonly.

Ex:

richTextBox.BackColor = Color.White;
richTextBox.ReadOnly = true; 
Tisho
  • 8,320
  • 6
  • 44
  • 52
shalini
  • 41
  • 1
3

Create a Custom Richtextbox as below This will produce a Richtextbox with a transparent Backcolor. You can then place this control on a suitably colored panel.

Public Class MyRichTextBox
Inherits RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim prams As CreateParams = MyBase.CreateParams
        If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
            prams.ExStyle = prams.ExStyle Or &H20 'Makes Transparent
            prams.ClassName = "RICHEDIT50W"
        End If
        Return prams
    End Get
End Property
Toby Twerl
  • 31
  • 2
2

its to late but its not a bad way,

    private void richTextBox1_ReadOnlyChanged(object sender, EventArgs e)
    {
        //just here instead of White select your color

        richTextBox1.BackColor = Color.White;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.ReadOnly = true;
    }
1

Take a look at DrawStringDisabled Method . You will have to override OnPaint method and then use DrawStringDisabled method. But, if I was at your place then I will go with Mikael Svenson's answer.

Community
  • 1
  • 1
P.K
  • 18,587
  • 11
  • 45
  • 51