Question about color

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

string fill = "Color.Salmon"
textBox1.BackColor = fill;

this doesn't work. What should I write after the = ?

(Please note that I am not wanting to write textBox1.
BackColor = Color.Salmon; I am wanting to fill some text boxes in a
loop and am reading the colors from a file.)

Many thanks.
 
Hi,

Take a look at Color.FromName. That should do what you want.

Hope this helps

Chris Taylor
 
string fillColor = "Salmon";

ColorConverter colorConverter = new ColorConverter();

textBox1.BackColor = (Color) colorConverter.ConvertFromString(fillColor);

Hope this helps

Rak
 
string fillColor = "Salmon";

ColorConverter colorConverter = new ColorConverter();

textBox1.BackColor = (Color) colorConverter.ConvertFromString(fillColor);

Hope this helps

Rak
That's great, thank you.
 
string fillColor = "Salmon";

ColorConverter colorConverter = new ColorConverter();

textBox1.BackColor = (Color) colorConverter.ConvertFromString(fillColor);
*********************************************
The code below produced the error:
Aquamarine is not a valid value for Int32
Colors like Aquamarine are the ones I want to diaplay.
How should I change the code?
Many thanks.
Patrick
*********************************************
private void Colors_Load(object sender, System.EventArgs e)
{
using (StreamReader sr = new StreamReader(@"Colors.txt"))
for(int i = 0; i<Controls.Count; i++)
{
Control mine = Controls;
if (mine is TextBox)
{
string fillColor = sr.ReadLine();/////// error line /////////
ColorConverter colorConverter = new ColorConverter();
mine.BackColor = (Color)colorConverter.ConvertFromString(fillColor);
}
}
}
 
string fillColor = "Salmon";

ColorConverter colorConverter = new ColorConverter();

textBox1.BackColor = (Color) colorConverter.ConvertFromString(fillColor);

Hope this helps

Rak

Actually, most of the 141 colors listed in "Color Members" in MSDN are
converted alright. It seems that we are talking about some forty valid
color members that won't convert this way.
 
Hi Patrick,

Chris suggested using the Color class:
Colour = Color.ColorFromName (sColour);

Rakesh suggested the longer:
ColorConverter colorConverter = new ColorConverter();
Colour = (Color) colorConverter.ConvertFromString (sColour);

=============
ColorFromName will take any string value. If it is valid it will set the
colour accordingly. If it is invalid, it will set the colour to Black. In both
cases it will set the name to whatever you give it (but if the name matches
characterwise (case insensitive), the standard name will be given).

These will produce the correct colour.
sColour Colour.ToString()
"Aquamarine" "Aquamarine" Perfect match
"aquaMarine" "Aquamarine" Conversion
"aquamarinE" "Aquamarine" Conversion

" Aquamarine " " Aquamarine " No change.
" aquaMarine " " aquaMarine " No change

This will produce Black.
sColour Colour.ToString()
"Zoltan" "Zoltan" Black by a different name.

Incorrect names will not produce an Exception.

=============
colorConverter.ConvertFromString (sColour) will only take an valid colour
name, although quotes and white space are allowed. The name will be converted
to the standard form. If the name is invalid an Exception will be thrown.

These will produce the correct colour.
sColour Colour.ToString()
"Aquamarine" "Aquamarine"
"aquaMarine" "Aquamarine" Conversion
"aquamarinE" "Aquamarine" Conversion
" Aquamarine " "Aquamarine" Conversion
" aquaMarine " "Aquamarine" Conversion

This will produce an Exception.
sColour
"Zoltan"

=============
My thinking is that there must be something wrong with the colour names in
your file, however, if you used cut-and-paste to show us the error with
Aquamarine, then something else is sadly amiss, for "Aquamarine" is a
perfectly valid name.

Regards,
Fergus

ps. You can move ColorConverter colorConverter = new ColorConverter(); out of
the loop - you don't need a fresh one for each conversion.
 
On Mon, 29 Sep 2003 19:40:45 +0100, "David Faircastle"

<snipped>

Thank you for your very generous reply. I need to sort things out now.
 
Back
Top