Question

I have a RichTextBox which I need to use as a Label for RichTextBox's have background selection which I need for my Windows Forms Application.

I have encountered a problem when disabling the RichTextBox, it has made the BackColor of the RichTextBox to a whitish/Grayish. I have tried using the following code:

ll.ForeColor = Color.LightGreen;
ll.Location = new Point(1, Form1.ActiveForm.Size.Height / 23 * 23);
ll.AutoSize = true;
ll.SelectionColor = Color.FromArgb(0, 0, 0);
ll.BorderStyle = BorderStyle.None;
ll.BackColor = Color.FromArgb(0, 0, 0);
ll.Multiline = false;
ll.ReadOnly = true;
ll.Enabled = false;
ll.SelectionBackColor = Color.Transparent;
ll.SelectAll();
ll.Font = new Font("ModeSeven", 12);

But still I get this... http://i.imgur.com/j098Fl6.png

Was it helpful?

Solution

i've just used the readOnly and not set the Enabled property as well property and got this:

enter image description here

which is exactly what you want i think.

here is the full code

     this.richTextBox1.Location = new System.Drawing.Point(31, 12);
     this.richTextBox1.Name = "richTextBox1";
     this.richTextBox1.ReadOnly = true;
     this.richTextBox1.Size = new System.Drawing.Size(100, 96);
     this.richTextBox1.TabIndex = 0;
     this.richTextBox1.Text = "tsglksklsmkld";
     richTextBox1.ForeColor = Color.LightGreen;
     richTextBox1.SelectionColor = Color.FromArgb(0, 0, 0);
     richTextBox1.BorderStyle = BorderStyle.None;
     richTextBox1.BackColor = Color.FromArgb(0, 0, 0);
     richTextBox1.SelectionBackColor = Color.Transparent;
     richTextBox1.Font = new Font("ModeSeven", 12);
     richTextBox1.Multiline = false;

if you want it to also not be selectable you can do:

this.richTextBox1.SelectionChanged += new System.EventHandler(this.richTextBox1_SelectionChanged);

and in the handler:

  private void richTextBox1_SelectionChanged(object sender, EventArgs e)
  {
     richTextBox1.Select(0,0);
  }

OTHER TIPS

Windows comes with themes and when you disable a control on a form, the control will show its status according to the current theme.
You cannot change how a disabled control looks like, for it will always behave according to the current theme.

What you can do, though, is to create a custom control, inherit from RTB and define your Paint-event accordingly.

See here for a thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top