We help IT Professionals succeed at work.

Delphi TColor held in DB  Table as string,  when i try to read value from DB Table i get an error

DaveAM
DaveAM asked
on
Medium Priority
906 Views
Last Modified: 2013-11-23
How can i get a colour value in a Database to be read by delphi as a TColor ie

The Value $00FF4AFF is held as a string in SQL server Table, but when i try to read the value from the DB table into program like
BorderColor:=dataModule1.Services_Table.FieldByName('Color_Value').AsString;
The following message appears
[DCC Error] Main.pas(280): E2010 Incompatible types: 'TColor' and 'string'
Comment
Watch Question

Commented:
Hi,

Check this link:

http://delphi.about.com/cs/adptips2001/a/bltip0301_5.htm


sColor := dataModule1.Services_Table.FieldByName('Color_Value').AsString;
BorderColor:= RGB(StrToInt('$'+Copy(sColor, 1, 2)), StrToInt('$'+Copy(sColor, 3, 2)), StrToInt('$'+Copy(sColor, 5, 2))) ;

/peter

Commented:
Sorry, missed the alpha info

BorderColor:= RGB(StrToInt('$'+Copy(sColor, 3, 2)), StrToInt('$'+Copy(sColor, 5, 2)), StrToInt('$'+Copy(sColor, 7, 2))) ;

Author

Commented:
Thanks

I can now get the item colored but it is the same color, irespective of the db entry

Commented:
Can you send the code where you like to color the item?

Author

Commented:
Sorry hope this helps

Thanks for all help
var
Sib, Sie, Sip, EndTime :Integer;//Sib Select Item Begin,  Sie Select Item End
TT, HBT              :Integer; //TT Total Time, HBT Hair Book Time
Bor_Color, Tra_Color, sColor  :string;
begin
 
Sib:=HairPlanner.SelItemBegin;
Sie:=HairPlanner.SelItemEnd;
Sip:=HairPlanner.SelPosition;
HBT:=datamodule1.Hair_Timing_Table.FieldByName('Hair_Book_Timing').Asinteger;
TT:= datamodule1.Hair_Services_Table.FieldByName('Time_Taken').AsInteger;
Bor_Color:=dataModule1.Hair_Services_Table.FieldByName('Color_Value').AsString;
sColor := dataModule1.Hair_Services_Table.FieldByName('Color_Value').AsString;
//Tra_Color:=dataModule1.Hair_Services_Table.FieldByName('Color_Value').CurValue;
Endtime:=(TT div HBT);
BDSTrue:=False;
      if NOT (HairPlanner.Items.HasItem(Sib,Sie,Sip) OR  HairPlanner.Items.HasItem(Sib,Sie,Sip))then
            with HairPlanner.CreateItemAtSelection do
              if StrToInt(ClientNoEdit1.Text)<=1 then
                  Begin
                    PlanItem2TF:=False;
                    PlanItem1TF:=False;
                    //BorderColor:=StringToColor (dbcolorV.Text);
                    BorderColor:= RGB(StrToInt('$'+Copy(sColor, 3, 2)), StrToInt('$'+Copy(sColor, 5, 2)), StrToInt('$'+Copy(sColor, 7, 2))) ;
                    //TrackColor:=dataModule1.Hair_Services_Table.FieldByName('Color_Value').Asstring;
                    ItemEnd:=ItemEnd + EndTime;
                    CaptionText:=HName.Text;
                    Text.Add(dataModule1.Hair_Services_Table.FieldByName('Short_Name').AsString);
                    Text.add(HTel.Text);
                    HairPlanner.Update;
                    Update;
                  End;
sColor:='';//I Just added this 
end;

Open in new window

Commented:
What is it you coloring? The border of HairPlannerItem (what type of component is it)? Does it always change to the same color? And you're sure there is different color values in db? Have you checked with messagebox, debugoutput or something?

Author

Commented:
sorry
The Planner  is a graphic representation of a diary page, each item is an appointment.  Each appointment dependent on service can have a set color as in the data base table, this can be set by the user from a color dialog box.  When an appointment is picked as in service, the color is then sent to th planner item as a border color

Commented:
I'm sorry, to me it looks alright, although I'm not familiar with the component. As I said please check that the return value from RGB to BorderColor is correct, either by MessageBox or Debugoutput.

Author

Commented:
Sorry Pivar
BorderColor is showing color if i mess with the RGB Values as in changing the  first value after sColor I can change the colors of the border but not to the color held in the DB Field
 BorderColor:= RGB(StrToInt('$'+Copy(sColor, 3, 2)), StrToInt('$'+Copy(sColor, 5, 2)), StrToInt('$'+Copy(sColor, 7, 2))) ;

Open in new window

Commented:
Can you break at the BorderColor := RGB(.... line and tell me what the value of sColor is?

Author

Commented:
The value of sColor is the same as the db field of the table.
I set an edit box to hold the contents of sColor  $009998FF  

Commented:
What is the value of BorderColor after assigning RGB?

Author

Commented:
Many thanks

the above delphi Tcolor $009998FF  appears as a grass green

Author

Commented:
Sorry Pivar just a follow up to your last question


All Tcolors appear as shades of green except red it appears as Black

Author

Commented:
Many thanks Pivar
After trial and error, not the best way to program though, I tried many combinations but this seemed to work best.  Please comment if you would make any changes
The colors appear true on screen.

RGB(StrToInt('$'+Copy(sColor, 7, 3)), StrToInt('$'+Copy(sColor, 5,3)), StrToInt('$'+Copy(sColor, 5,2))) ;

thanks again in advance.

Commented:
I would not recommend you to do like that. Because you're not creating correct colors according to the rgb standard that you have stored in the db. If you think the colorvalues in the db are wrong, then change it in the db and use the correct rgb creation command:
 
BorderColor:= RGB(StrToInt('$'+Copy(sColor, 3, 2)), StrToInt('$'+Copy(sColor, 5, 2)), StrToInt('$'+Copy(sColor, 7, 2))) ;

This would give a TColor 16750745  with the value $009998FF and it should be light blue.
Commented:
Can you not use the functions from Graphics unit as below?...., they work fine for me.....

read from dataset...
        ReadOnlyColor := StringToColor(FieldByName('read_only_color').AsString);
and set by stored proc...
        ParamByName('@read_only_color').Value := ColorToString(ReadOnlyColor);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Pivar, had to get my head round the graphics and colour settings, but everything works great.  I have now written an SQL statement to pull from the database

Author

Commented:
Sorry on last comment many thanks Bongos, I do appologise.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.