大家好!问题如下所述:一个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 控件。

解决方案 »

  1.   

    if i = 2 then
    begin
      caption := inttostr(i);
      break; //  這樣再試試
    end;
      

  2.   

    timer1.Enabled := false ;//只是把Timer停止, for 並不支跳出呀.
      

  3.   

    if i = 2 then
    begin
      caption := inttostr(i);
      if not TTimer(Sender).Enabled  then
        break; //  這樣再試試
    end;
      

  4.   

    欢迎加入Borland DELPHI程序员,参与群里技术讨论!欢迎女孩子,也欢迎男孩子参与技术讨论!群号15154361
      

  5.   

    把 i 定义为全局变量
    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;
      

  6.   

    无论你当 i = ? 时
    你单击 Button 就可以停了Timer
      

  7.   

    你只有在for语句中添加判断timer.enabled了
      

  8.   

    //Timer 事件没有处理完成
    //Click 事件是不会处理的,for 
      application.handlemessage
    end;
      

  9.   

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

  10.   

    var
      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所以你也无法做任何事,当然按按扭是不管用的。