I have a textBox that I with help of a Button turning to a Gray BackColor like this.

textBox1->BackColor = Color::Gray;

Under this textBox I have a checkBox. (with default "checked").
What I am trying to do here is that when I "uncheck" this checkBox, the textBox will turn "Red" like this:

textBox1->BackColor = Color::Red;

The problem and what I want to do is that when I "check" the checkBox again, I want the textBox1 to turn to the color that from the beginning was in the textBox wich in this case was "Gray".
So I wonder if it is possible to use a code that saves the color in the textBox to a String^ before the textBox1 turns "Red" like below:
Though I know that I probably cant save the current color in the textBox to a String^.

Why I want to save the color before "unchecking" the checkBox is that when checking it again, I have saved that color in this case "Gray" somewhere to use when I "check" the checkBox again to set the color that was from the beginning.
Is it possible to save the BackColor in anyway, to a String^ or something else ?

String^ SaveCurrentColor = textBox1->BackColor; //This is not correct
textBox1->BackColor = Color::Red;

Recommended Answers

All 3 Replies

I have tried to save the BackColor in a textBox in a string like this:

String^ SaveCurrentColor = textBox1->BackColor.ToString()

If "SaveCurrentColor" holds a colorvalue for example "Gray" in this case,
Is it possible to use "SaveCurrentColor" in a code to turn the BackColor in
textBox1 to "Gray" ?

For example instead of specify the color like this: = Color::Gray to do something like this, though I know this is wrong:
(First set the color to a String^ and then use the String^ to set the color to textBox1 again)

String^ SaveCurrentColor = textBox1->BackColor.ToString()
textBox1->BackColor = Color::SaveCurrentColor; //This is wrong

I think you should forget trying to use a String to store and restore a color.
Instead, you can simply use a member variable of type System::Drawing::Color to save the original color and restore it back when necessary.

So in your Form class, add a member variable like; System::Drawing::Color ColorSaved; Then when you uncheck the checkbox, save the current color first

ColorSaved = textBox1->BackColor;
textBox1->BackColor = Color::Gray;

Then when you check the unchecked checkbox, restore the saved color textBox1->BackColor = ColorSaved;

Thanks for this approach. This works fine

/Jennifer

I think you should forget trying to use a String to store and restore a color.
Instead, you can simply use a member variable of type System::Drawing::Color to save the original color and restore it back when necessary.

So in your Form class, add a member variable like; System::Drawing::Color ColorSaved; Then when you uncheck the checkbox, save the current color first

ColorSaved = textBox1->BackColor;
textBox1->BackColor = Color::Gray;

Then when you check the unchecked checkbox, restore the saved color textBox1->BackColor = ColorSaved;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.