views:

882

answers:

4

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?

+7  A: 

See: http://stackoverflow.com/questions/276179/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(); };
    }
}
Mikael Svenson
its not the thing i asked for!
moon
@moon: Yes it is.
Henk Holterman
@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
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!
moon
Added code example on how you can get it to work so that the cursor won't blink.
Mikael Svenson
A: 

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.

P.K
A: 

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
A: 

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