Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a ReadOnly TextBox default BackColor

I have a TextBox which is set to be ReadOnly.
At some point that TextBox is being available for editing, and it's BackColor changes (It is indicating if the value is valid).
If I want to set the TexBox back to ReadOnly, the TextBox doesn't get back the original BackColor that a ReadOnly TextBox gets.
What should I do in order to get the original color again?
I realize I can set the color manually to SystemColors.Control, but is this the "right way"?

Code Sample

This is a simple code for demonstration. If SystemColors.Control is the way to go, I will change it in the ReadOnlyChanged event...

private void button1_Click(object sender, EventArgs e)
{
    //At this point this.textBox1 is ReadOnly
    this.textBox1.ReadOnly = false;
    this.textBox1.BackColor = Color.Orange;


    /*this.textBox1.BackColor = SystemColors.Control;*/ //Is this the right way?
    this.textBox1.ReadOnly = true; //Textbox remains orange...
}
like image 331
Avi Turner Avatar asked Aug 04 '13 05:08

Avi Turner


People also ask

How do I make a TextBox read only?

Set the TextBox control's ReadOnly property to true . With the property set to true , users can still scroll and highlight text in a text box without allowing changes.

How do you change the BackColor in Visual Basic?

To change the background color, select the form in Visual Studio and locate the BackColor property in the Properties panel. There are a number of ways to specify a color. Color by name - Simply type in a color name into the BackColor value field (for example Red, Yellow, Cyan etc).

What is the default background color of TextBox?

It's white by default in my browsers , but I wonder if it's reliable to just leave it as is and be sure other browsers render it white.


3 Answers

I know this is an old question, but for posterity sake:

TextBox as well as many other controls rely on Color.Empty to decide whether or not to display its default color.

To set a TextBox back to the system default (irregardless of state):

textBox1.BackColor = Color.Empty;
like image 193
Rhaokiel Avatar answered Oct 22 '22 02:10

Rhaokiel


You have to set BackColor to the look of a ReadOnly TextBox's BackColor, that is Color.FromKnownColor(KnownColor.Control):

//this is the ReadOnlyChanged event handler for your textbox
private void textBox1_ReadOnlyChanged(object sender, EventArgs e){
   if(textBox1.ReadOnly) textBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}

You may need a variable to store the current BackColor every time your TextBox's BackColor changes:

Color currentBackColor;
bool suppressBackColorChanged;
private void textBox1_BackColorChanged(object sender,EventArgs e){
   if(suppressBackColorChanged) return;
   currentBackColor = textBox1.BackColor;
}
private void textBox1_ReadOnlyChanged(object sender, EventArgs e){
   suppressBackColorChanged = true;
   textBox1.BackColor = textBox1.ReadOnly ? Color.FromKnownColor(KnownColor.Control) : currentBackColor;
   suppressBackColorChanged = false;
}
like image 20
King King Avatar answered Oct 22 '22 01:10

King King


Yes, that's fine. There's no reason you can't use the SystemColors to specify the desired color for the control. I've never heard of anything in WinForms that would cause a control to automatically revert to its default color upon setting ReadOnly = true.

I suppose one alternative is to create a class-level variable called textBox1OriginalColor or something and set it in the form's Load event. Then you know exactly what it was when the form was originally displayed, if you think someone might in the future set the text box's default background color to, say, blue in the designer or something.

like image 43
Grant Winney Avatar answered Oct 22 '22 01:10

Grant Winney