简体   繁体   中英

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?

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

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

Ex:

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM