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.