Hallo!!!
folgendes Problem:
in einer meiner Prozeduren funktioniert eine IF-ELSE Anweisung nicht richtig.
Wenn IF-Anweisung
falsch ist, muss er doch zu ELSE-Anweisung springen. Tut er aber nicht. Er macht was unerwünschtes: Wenn IF-Anweisung
wahr ist, führt er die Zeile 6 aus und springt in den BEGIN-Block (Zeile

, denn ich eigentlich für ELSE programmiert habe. Wo ist der Fehler?
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
| procedure TForm1.Count(); var i:Integer; begin for i:=1 to 12 do if FindComponent('Shape'+IntToStr(i)) <> nil then Anzahl:=i else begin Anzahl:=0; break; end; end; |
hier der ganze Code:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls;
type TForm1 = class(TForm) Timer1: TTimer; Shape1: TShape; Shape2: TShape; Stopline: TShape; green: TCheckBox; Shape3: TShape; Button1: TButton; Memo1: TMemo; procedure Timer1Timer(Sender: TObject); procedure Button1Click(Sender: TObject); private procedure move(i:Integer); procedure Count(); function Distance(i:Integer): boolean; public end;
var Shray: array of TShape; Form1: TForm1; Anzahl: Integer;
implementation
{$R *.dfm}
function TForm1.Distance(i:Integer): boolean; begin Distance:=(((FindComponent('Shape'+IntToStr(i)) as TShape).Left+ 2*(FindComponent('Shape'+IntToStr(i)) as TShape).width < (FindComponent('Shape'+IntToStr(i-1)) as TShape).Left)) end;
procedure TForm1.move(i:Integer); begin (FindComponent('Shape'+IntToStr(i)) as TShape).left:= (FindComponent('Shape'+IntToStr(i)) as TShape).left+1; end;
procedure TForm1.Count(); var i:Integer; begin for i:=1 to 12 do if FindComponent('Shape'+IntToStr(i)) <> nil then Anzahl:=i else begin Anzahl:=0; break; end; end;
procedure TForm1.Timer1Timer(Sender: TObject); var i,g:Integer;
begin Count(); for i:=1 to Anzahl do if FindComponent('Shape'+IntToStr(i)) <> nil then begin if FindComponent('Shape'+IntToStr(i-1)) <> nil then begin if Distance(i) then move(i) else end else if (FindComponent('Shape'+IntToStr(i)) as TShape).Left<607 then move(i) else if (green.Checked=true) then if (FindComponent('Shape'+IntToStr(i)) as TShape).Left>680 then begin FindComponent('Shape'+IntToStr(i)).Destroy; Count(); for g:=1 to (Anzahl-1) do FindComponent('Shape'+IntToStr(g+1)).Name:='Shape'+IntToStr(g); break; end else move(i) end; end;
procedure TForm1.Button1Click(Sender: TObject);
var Shape : TShape; begin
Shape := TShape.Create(Form1); With Shape do begin Parent := Form1; Left := 0; Top := 272; Width:= 73; Height:=41; Name := 'Shape'+IntToStr(Anzahl+1); end; Count(); end;
end. |