0

I want to change the font color of a TLabel, based on the value that i got from an Integer.

var
  i: Integer;
begin
 ValueGenerator (i);
  if i <= 14 then
  begin
    L1.Font.Color := clBlue;
  end;
  if i >= 15 then
  begin
    L1.Font.Color := clGreen;
  end;
  if i <= 29 then
  begin
    L1.Font.Color := clGreen;
  end;
  if i >= 30 then
  begin
    L1.Color := clYellow;
  end;
  if i <= 49 then
  begin
    L1.Color := clYellow;
  end;
  if i >= 50 then
  begin
    L1.Color := clRed;
  end;
  L1.Caption := IntToStr(i);
end;

The value output is from 0 to 100. What ever value I got, my TLabel is always on clGreen. What I did wrong here?

5
  • The ranges overlap. If you give it the value 20, it's going to be both greater than 15 and less than 29. You should consider adding some else in there to make it pick only one, but even so I would restructure this code. Perhaps a case is what you really want? Commented Jul 23, 2014 at 18:31
  • You need more else. Commented Jul 23, 2014 at 18:33
  • Yes, with more else is working. But as i saw 'case' solution, it is the best for my need. Thank you @LasseV.Karlsen Commented Jul 23, 2014 at 18:46
  • 2
    What prevented you from using the debugger to answer this question for yourself? Commented Jul 23, 2014 at 19:51
  • @RobKennedy Thank you, I will be more intense to use debugger from now on. Never looked at it seriously before. Commented Jul 27, 2014 at 11:26

3 Answers 3

7

Your ranges overlap, the value 20 is both greater than 15 and less than 29, so it first sets one color, and then another, but none of them makes the rest of the code not execute. So First you set clGreen, and then later you have the case where i is less than 49, which will overwrite.

Here's how you probably want to write that code:

case i of
    0..14: L1.Font.Color := clBlue;
    15..29: L1.Font.Color := clGreen;
    30..49: L1.Font.Color := clYellow;
    50..100: L1.Font.Color := clRed;
end;
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what i want. case things is new to me. Thank you so much for the code! BR, Bee.
6

Instead of case I would use in this case some stacked if .. then .. else, so you do not have to take care for the whole range of each color, just for the maximum value

if i <= 14 then
  L1.Color := clBlue
else if i <= 29 then
  L1.Color := clGreen
else if i <= 49 then
  L1.Color := clYellow
// can easily extended with
// else if i <= 79 then
//   L1.Color := clMaroon
else
  L1.Color := clRed;

9 Comments

'case' has an 'else' too.
@SertacAkyuz Yes, but my answer has no case
I know. You gave an advantage "so you do not have to take care for the whole range", you can do that with a 'case' too. That's what I was saying.
@SertacAkyuz Using case you have to define the range for each color 0..14, 15..29, ... if you want to adjust the range for one color you have to edit two ranges
Ok, I thought it was about not including the '100'.
|
0

your color will always be either blue or green based on your logic. you need to add the top condition:

if (i >= 15) and (i < 30)  then
begin
    L1.Font.Color := clGreen;
 end;

and so on and so forth.

1 Comment

Thank you, this approach is so-so as case above. Fixed value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.