大家好!问题如下所述:一个Form1窗体,窗体上一个Timer1控件,一个Button1 控件。代码如下:procedure TForm1.Timer1Timer(Sender: TObject);
var
i : integer ;
begin
for i:=0 to 4 do
begin if i= 0 then
caption := inttostr(i); if i = 1 then
caption := inttostr(i); if i = 2 then
caption := inttostr(i); if i = 3 then
caption := inttostr(i); if i = 4 then
caption := inttostr(i); sleep(1000);
endend;我的Timer1控件的interval属性是:7000毫秒 。其中还有一个Button1控件的Click 事件如下:procedure TForm1.Button1KeyPress(Sender: TObject; var Key: Char);
begin
timer1.Enabled := false ;
end;我的问题是:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 我想在 i = 2 的时候,跳出循环。我点击Button1 按钮怎么终止不了Timer1 控件啊????
为什么当时点击了不管用啊?????直到运行到 i = 4 时才终止。为什么啊 ????????????????如果我还想 i 等任何值的时候中指循环请问怎么做请大家帮忙!!!!!!!!!!!!!!!!!!!一定要用到Timer1 控件。
var
i : integer ;
begin
for i:=0 to 4 do
begin if i= 0 then
caption := inttostr(i); if i = 1 then
caption := inttostr(i); if i = 2 then
caption := inttostr(i); if i = 3 then
caption := inttostr(i); if i = 4 then
caption := inttostr(i); sleep(1000);
endend;我的Timer1控件的interval属性是:7000毫秒 。其中还有一个Button1控件的Click 事件如下:procedure TForm1.Button1KeyPress(Sender: TObject; var Key: Char);
begin
timer1.Enabled := false ;
end;我的问题是:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 我想在 i = 2 的时候,跳出循环。我点击Button1 按钮怎么终止不了Timer1 控件啊????
为什么当时点击了不管用啊?????直到运行到 i = 4 时才终止。为什么啊 ????????????????如果我还想 i 等任何值的时候中指循环请问怎么做请大家帮忙!!!!!!!!!!!!!!!!!!!一定要用到Timer1 控件。
begin
caption := inttostr(i);
break; // 這樣再試試
end;
begin
caption := inttostr(i);
if not TTimer(Sender).Enabled then
break; // 這樣再試試
end;
var
i: integer;//Timer 事件
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if i < 4 then Inc(i)
else i := 0; if i= 0 then
caption := inttostr(i); if i = 1 then
caption := inttostr(i); if i = 2 then
caption := inttostr(i); if i = 3 then
caption := inttostr(i); if i = 4 then
caption := inttostr(i); sleep(1000);
end;//Button 单击事件
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Timer1.Enabled := False;
// Caption := Caption + 'The Timer Enable is False';
end;
你单击 Button 就可以停了Timer
//Click 事件是不会处理的,for
application.handlemessage
end;
Stop: Boolean;procedure TForm1.Timer1Timer(Sender: TObject);
var
i : integer ;
begin
for i:=0 to 4 do
begin if i= 0 then
begin
caption := inttostr(i);
if Stop then Exit;
end; if i = 1 then
begin
caption := inttostr(i);
if Stop then Exit;
end; if i = 2 then
begin
caption := inttostr(i);
if Stop then Exit;
end; if i = 3 then
begin
caption := inttostr(i);
if Stop then Exit;
end; if i = 4 then
begin
caption := inttostr(i);
if Stop then Exit;
end; sleep(1000);
endend;procedure TForm1.Button1KeyPress(Sender: TObject; var Key: Char);
begin
Stop := True;
timer1.Enabled := false ;
end;
Form1: TForm1;
i:integer;
implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
begin
if i>4 then i:=0 else i:=i+1;
if timer1.Enabled then
caption := inttostr(i);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Enabled := false ;end;
如果使用sleep表示当前线程sleep所以你也无法做任何事,当然按按扭是不管用的。