RichTextBox.Enabled = False forces unwanted background color

  • Thread starter Thread starter ChrisUttenreither
  • Start date Start date
C

ChrisUttenreither

Hello folks. I'm writing a Roguelike game in C#. It contains a
RichTextBox which displays a map. The map is made up of multicolored
ASCII art. Since this control is displaying a map I do not want a
blinking cursor on it, so I set the Enabled property to false.
Unfortunately this forces the control's background color to be gray.
You can't see the map very well due to this.

I have already tried making a new control called MapDisplayTextBox
which inherets from RichTextBox. I overrode the OnBackColorChanged
event to force the background to be black. A MessageBox is displayed
in the event handler to verify that this took place. It reports the
color is black. However the background of the control is gray. When
you examine the BackColor property directly it returns Color.Black. It
leads me to believe that this behavior is determined somewhere outside
of the class.

There are two possible solutions that I see to my problem.

- Find a way to have a nonEnabled RichTextBox with a background color
of my choosing.
- Find a way to eliminate the blinking carat from an Enabled
RichTextBox.

Thank you very much for your help.
 
Hello,

I didn't test but I suppose the cursor is not displayed when your box
doesn't have the focus, so you can simply reject the focus or give the focus
to another control each time your RichTextBox get it.
 
Thanks for the response. I don't have any other controls to give focus
to. Since there is no Cancel property in EventArgs which comes in the
Enter event I am not sure how to reject focus. How do you suggest I do
that?
 
Please find a little trick to disable focus :

public class myRichTextBox: RichTextBox
{

protected override void WndProc(ref Message m)
{
const int WM_SETFOCUS = 0x0007;
if (m.Msg == WM_SETFOCUS)
{
m.Result = IntPtr.Zero;
}
else
base.WndProc(ref m);
}
}

You just have to create a myRichTextBox object in place of your standard
RichTextBox.

Enjoy & let me know :D
 
Back
Top