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

33,396

Solution 1

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(); };
    }
}

Solution 2

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

Solution 3

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

Ex:

richTextBox.BackColor = Color.White;
richTextBox.ReadOnly = true; 

Solution 4

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

Solution 5

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;
    }
Share:
33,396
Badr
Author by

Badr

I am a java enthusiast with 6 years of J2EE development experience, it includes development of custom frameworks and working with famous frameworks like Spring, Hibernate, Jersey etc. I also love to work as a full stack developer with Java as server side technology and HTML5, CSS3 and Javascript for front end development. For front end I like work with angular.js and backbone.js and for styling My choice is less css. I am reachable at [email protected]

Updated on September 17, 2020

Comments

  • Badr
    Badr over 3 years

    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?

  • Badr
    Badr over 14 years
    its not the thing i asked for!
  • Mikael Svenson
    Mikael Svenson over 14 years
    @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.
  • Badr
    Badr over 14 years
    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!
  • Mikael Svenson
    Mikael Svenson over 14 years
    Added code example on how you can get it to work so that the cursor won't blink.
  • Adam K Dean
    Adam K Dean about 11 years
    Doesn't quite work the same, readonly still allows selection, scrolling, etc.
  • Jon Coombs
    Jon Coombs over 10 years
    +1 because in my situation the enable/disable behavior seemed unreliable when ShowSelectionMargin was set to True.
  • musicinmusic
    musicinmusic almost 5 years
    The question is about disabling the richTextBox control, not setting it as a readOnly control.
  • denver
    denver almost 4 years
    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
    denver almost 4 years
    The other answers produce finicky results.