voila je voudrais savoir si on peut reduire le code si dessous ?
procedure TForm2.Timer1Timer(Sender: TObject);
begin
if label4.Caption=inttostr(0) then label4.Font.Color:=clred;
if label4.Caption=inttostr(1) then label4.Font.Color:=clred;
if label4.Caption=inttostr(2) then label4.Font.Color:=clred;
if label4.Caption=inttostr(3) then label4.Font.Color:=clred;
if label4.Caption=inttostr(4) then label4.Font.Color:=clgreen;
if label4.Caption=inttostr(5) then label4.Font.Color:=clgreen;
Je voudrais juste qu'entre 0 et 3 le label deviens rouge et si il affiche 4 et 5 qu'il devient vert
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with Label4 do
begin
if StrToInt(Caption) in [0..3] then
Font.Color := clRed else
if StrToInt(Caption) in [4, 5] then
Font.Color := clGreen;
end
end;
ou encore :
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with Label4 do
case StrToInt(Caption) of
0..3: Font.Color := clRed;
4, 5: Font.Color := clGreen;
end;
end;
etc...
N'oublie pas de protéger le code si risque de valeur imprévue retournée par Label.Caption (utilisation de "StrToIntDef", rajout de "else" dans "case of"...).
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with Label4 do
begin
if Caption[1] in ['0'..'3'] then
Font.Color := clRed else
if Caption[1] in ['4', '5'] then
Font.Color := clGreen;
end
end;
Salut
Tu peux aussi essayer cela :
if pos(label4.caption,'0123') > 0
then label4.Font.Color:=clred
else if pos(label4.caption,'45') > 0 then label4.Font.Color:=clgreen;