Racourcir un bout de code

Résolu
ced55957 Messages postés 108 Date d'inscription dimanche 2 mars 2003 Statut Membre Dernière intervention 19 juin 2012 - 28 févr. 2012 à 14:02
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 - 9 mars 2012 à 03:13
bonjour

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

merci par avance

6 réponses

ced55957 Messages postés 108 Date d'inscription dimanche 2 mars 2003 Statut Membre Dernière intervention 19 juin 2012
28 févr. 2012 à 17:10
oula oui j'etais parti très loin

merci ça marche impeccable Korgis
3
ced55957 Messages postés 108 Date d'inscription dimanche 2 mars 2003 Statut Membre Dernière intervention 19 juin 2012
28 févr. 2012 à 15:40
j'ai essayer comme ceci :

procedure TForm2.Timer1Timer(Sender: TObject);
var
i:set of 0..15;
begin
i:= [0..15];

if label4.Caption=i then label4.Font.Color:=clred;


mais forcement ça marche pas il me manque un truc
0
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
28 févr. 2012 à 16:09
C'est très basique.

par exemple :

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"...).
0
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
28 févr. 2012 à 16:17
Et pourquoi pas, d'ailleurs :

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;
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
28 févr. 2012 à 17:36
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;

Dubois77
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
9 mars 2012 à 03:13
const
  CapMax  = 5;
  Cap2Col : array[0..CapMax-1] integer = (
    clRed, clRed, clRed, clGreen, clGreen
  );

procedure TForm2.Timer1Timer(Sender: TObject);
begin
  with Label4 do
  begin
    Font.Color := Cap2Col[Tag];
    Tag        := (Tag + 1) mod CapMax;
    Caption    := IntToStr(Tag);
  end;
end;


________________________________________________________
besoin de câbles audio, vidèo, informatique pas cher ?
0
Rejoignez-nous