Sortir de la procedue avant la fin

promenneur77 Messages postés 16 Date d'inscription lundi 25 août 2003 Statut Membre Dernière intervention 4 mai 2008 - 23 janv. 2008 à 23:07
Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 - 25 janv. 2008 à 09:08
je voudrais mettre fin au programme quand il m'affiche ' pas dans la liste'

comment je doit faire ?

procedure TForm1.Button1Click(Sender: TObject);
var
Contents:TStringList;
Contents2:TStringList;
find:boolean;
i:integer;
MonChamp:String;
k : integer;
Handle : Thandle;
begin
  find:=false;
  Contents:=TStringList.Create;
  try
    Contents.LoadFromFile('D:\install\liste.txt');
    i:=0;
    repeat
     if Pos(Edit1.Text, Contents[i]) > 0then
       find:=true
     else
       i:=i+1;
     until((find)or(i=Contents.Count));
      if find then
      MonChamp:=Contents[i];
        if AnsiContainsText(MonChamp, Edit1.text)then
           begin
            Edit2.text := RightStr(Monchamp,Length(MonChamp)- Pos(' ',MonChamp));
            Exit;  
           end
      else
      ShowMessage('pas dans la liste')
        finally
    Contents.free;
  Contents2:=TStringList.Create;
  try
        begin
    Contents2.LoadFromFile('D:\loyalty\config\loyalty tampon.txt');
           end;
         begin
          For k:=1to Contents2.Count-1do
            begin
               Contents2[k]:=StringReplace(Contents2[k],'X.X.X.X',Edit2.text,[rfReplaceAll]);
            end;
          Contents2.SaveToFile('D:\loyalty\config\loyalty.properties');
           end;
            finally
             Contents2.free;
             end;
       begin
       shellexecute(Handle,'open','D:\Loyalty\gui.bat',nil,nil,sw_show);
      end;
   end;
end;
end.

7 réponses

Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 6
23 janv. 2008 à 23:54
   i:=0;
   repeat
     if Pos(Edit1.Text, Contents[i]) > 0then
       find:=true
     else
       i:=i+1;
     until((find)or(i=Contents.Count));


que se passe t'il si Contents.count = 0?

essaye plutot
find :=false;
i := 0;
while (not find) and (i < Contents.count) do
begin
    if Pos(Edit1Text, Content[i]) > 0 then
       find := true
    else
        Inc(i);
end;

pour arreter le programme, tu peux utiliser
Application.Terminate;

par contre, je suis pas sur que le finally soit executé si tu fais un exit !
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
24 janv. 2008 à 01:27
var
  fnd      : boolean;
  i        : integer;
  MonChamp : String;
begin
  with TStringList.Create do
  try
    LoadFromFile('D:\install\liste.txt');
    i   := 0;
    fnd := false;
    while (not fnd) or (i < count) do
    begin
      fnd := Pos(Edit1.Text, Strings[i]) > 0;
      if fnd then
        MonChamp := Strings[i];
      i := i + 1;
    end;
  finally
    free;
  end;

  if AnsiContainsText(MonChamp, Edit1.text) then
  begin
    Edit2.text := RightStr(Monchamp, Length(MonChamp)-Pos(' ',MonChamp));
    // exit;
  end
  else
  begin
    ShowMessage('pas dans la liste')
    // exit;
  end;

  with TStringList.Create do
  try
    LoadFromFile('D:\loyalty\config\loyalty tampon.txt');
    Text := StringReplace(Text, 'X.X.X.X', Edit2.text, [rfReplaceAll]);
    SaveToFile('D:\loyalty\config\loyalty.properties');
  finally
    Contents.free;
  end;

  shellexecute(0, 'open', 'D:\Loyalty\gui.bat', nil, nil, sw_show);
end.








<hr size="2" width="100%" />


http://deefaze.gnomz.com
0
Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 6
24 janv. 2008 à 09:03
@foxi :
while (not fnd) or (i < count) do // MonChamp = dernier element de la liste correspondant (inutile, autant partir de la fin de la liste)
while (not fnd) and (i < count) do // MonChamp = premier element de la liste correspondant
0
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
24 janv. 2008 à 11:16
@Guillemouze :
"par contre, je suis pas sur que le finally soit executé si tu fais un exit !"
Ben si ...
une simple petit essai te le prouvera en mettant un point d'arrêt dans la partie Finally

Par contre je ne suis pas trop d'accord avec :
"pour arreter le programme, tu peux utiliser
Application.Terminate;"

si tu as des instructions dans le OnClose elles ne seront pas exécutés.

Il est préférable de faire appel à Close si tu es dans la MainForm sinon il faut faire appel à Application.MainForm.Close;

Comme pour l'autre un petit teste te le prouvera en mettant dans le OnClose un ShowMessage et en testant les deux méthodes (Terminate & Close)

 
@+
Cirec

<hr siz="" />
0

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

Posez votre question
Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 6
24 janv. 2008 à 12:58
@cirec: merci pour l'info, j'aurai bien testé au moment ou j'ai ecrit le message mais j'avais pas delphi sous la main.
Et je prend bien en note l'histoire du OnClose, meme si je n'utilise jamais application.terminate car je trouve que son utilisation est liée a une lacune dans la conception du programme.
0
cs_Delphiprog Messages postés 4297 Date d'inscription samedi 19 janvier 2002 Statut Membre Dernière intervention 9 janvier 2013 32
24 janv. 2008 à 23:25
Guillemouze a écrit :
"je n'utilise jamais application.terminate car je trouve que son utilisation est liée a une lacune dans la conception du programme."

J'approuve entièrement et j'applaudis des deux mains
Puisse-t-il être entendu par tous ceux qui fréquentent ce site....

May Delphi be with you !
<hr color="#008000" />
Pensez à cliquer sur Réponse acceptée lorsque la réponse vous convient.
http://www.afipa.net/
0
Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 6
25 janv. 2008 à 09:08
merci delphiprog, ta remarque me va droit au coeur

Delphiprog a ecrit :
"J'approuve entièrement et j'applaudis des deux mains"
applaudir à une main ne doit pas etre facile ;)
0
Rejoignez-nous