Question

I am creating a Delphi form and want to assign background color to it on run time. This code:

Form1.Color := clSkyBlue; 

works well. My problem is that I have stored the color name in a table as a string. So, when I fetch the color name from the table and assign it to Form1.Color, the above statement becomes:

var
  ColorName: string;
....
Form1.Color := ColorName;

and this results in a compile time error.

How can I convert a string to a TColor value?

Was it helpful?

Solution

You can use the following conversion functions (both works with color constants):

  • the StringToColor function use to translate a string representation of a color to TColor value
  • the ColorToString function use when you need to translate TColor value to a string representation

In your case you need to use the StringToColor function:

Form1.Color := StringToColor('clSkyBlue');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top