Jump to content
Bart Verbakel

W1071 when assigning color to a control

Recommended Posts

Hello all,

 

I want to change to color of a combobox, depending of the contents of it.

If CB1 and CB2 contain the same value, the color of CB1 will be orange, if not the color will be default (ClWindow)

 

procedure TForm1.ComboBox1Change(Sender: TObject);
var ClOrange:ColorRef;
begin
  ClOrange:=RGB(255,160,0);
  if Combobox1.Text=Combobox2.Text
    then Combobox1.Color:=ClOrange
      else Combobox1.Color:=ClWindow;
end;

 

This code works OK, but I got a warning during compling:

W1071 Implicit integer cast with potential data loss from 'Cardinal' to 'TColor'

 

It feels like my code is not 100% correct , but I don't understand the issue.

I tried conversions to RGB or a StringToColor conversion, but all in vain,

 

How can I prevent this hint from popping up (and not disabeling it in the options 😉 )

 

With kind regards,

Bart

 

 

 

 

 

Share this post


Link to post
1 hour ago, Bart Verbakel said:

var ClOrange:ColorRef;

 

Change that to

Var 
  clOrange: TColor;

 

Share this post


Link to post

Alternatively, there are many color constants defined in the Vcl.Graphics and System.UITypes units.

 

The Vcl.Graphics.clWebOrange constant maps to the System.UITypes.TColorRec.Orange constant, which has a numeric value of $00A5FF (ie, RGB(255, 165, 0)), which is almost identical to your desired RGB color.

Share this post


Link to post

How would changing the variable from ColorRef to TColor help? That will only change the location of the warning because RGB returns ColorRef (given this is the function from Winapi.Windows.pas).

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×