for m:= 1 to 100    do
beginss:=Format('%.6d',[m]);
sleep(50);
         label1.caption:=ss;end;label1.caption 为什么不能逐一显示啊,只能显示100。请教各位了。

解决方案 »

  1.   

    VCL框架是单线程运作的,界面的更新是消息驱动的。在你的代码段里面,更新了lable的caption,理论上应当就立即显示在界面上的。
    但是这个时候你当前的代码片段尚未结束退出,消息泵无法发送lable的caption更新消息的,直到你的代码片段结束,才轮到消息泵发送lable的更新消息,此时当然直接更新最后一个标题文字咯要想达到你的效果,普遍做法是在你的循环代码片段里面,每步发出Application.ProcessMessage,当前代码片段挂起,主动呼叫消息泵处理队列里面的消息,处理完毕再回到你的代码片段,有点类似dos下的中断转移及中断现场的恢复。VCL消息泵循环处理
    while 不是WM_QUIT的消息 do
    begin
    从消息队列捡取一条消息
    发送到消息的窗口过程
    窗口过程找到这条消息的处理句柄
    (也就是你用的什么OnClick,OnTimer,OnShow,OnClose)等等句柄处理 
                |
                |
      这个代码片段就是你的循环代码片段,你的代码片段没有结束退出,这个消息泵无法执行下一个while消息处理判断,当然就无法更新lable的caption
    end
      

  2.   

    二楼已经说的够详细了  for m := 1 to 100 do
      begin
        ss := Format('%.6d', [m]);
        sleep(50);
        label1.caption := ss;
        Application.ProcessMessages;
      end;
      

  3.   


    for m:= 1 to 100 do
    beginss:=Format('%.6d',[m]);
    sleep(50);
    label1.caption:=ss;
    application.ProcessMessages;////
    end;
      

  4.   

    好的,谢谢了,我等会试试。yield()是什么函数?
    application.ProcessMessages;//用了这个还要用yield吗?
      

  5.   

    看效果:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        Timer1: TTimer;
        procedure Timer1Timer(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1:TForm1;
      i:integer;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Inc(i);
      if i>100 then i:=1;
      label1.caption:=IntToStr(i);
      if i=100 then Timer1.Enabled:=False;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not Timer1.Enabled then
      begin
        i:=0;
        Application.ProcessMessages;
        Timer1.Interval:=100;
        Timer1.Enabled:=true;
      end;
    end;end.
      

  6.   

    回覆如下:Yield:
    在powerbuilder 中有一個函數yield();用法如下:
    for i = 1 to n 
       yield()
       ....
       ....
    next
    這樣在迴圈過程中及時兼顧系統操作,譬如拷貝檔,各種按鍵等都是可以啟動的。它在Delphi所對應的函數類似於:Application.ProcessMessages;如果你運行一個非常耗時的迴圈,那麼在這個迴圈結束前,你的程式可能不會回應任何事件,你按按鈕沒有反應,程式設置無法繪製表單,看上去就如同死了一樣,這有時不是很方便,例如於終止迴圈的機會都沒有了。這時你就可以在迴圈中加上這麼一句,每次程式運行到這句時,程式就會讓系統回應一下消息,從而使你有機會按按鈕,表單有機會繪製...
      

  7.   

    顯示循環進度,增加一個進度條:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, ComCtrls;type
      TForm1 = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        Timer1: TTimer;
        ProgressBar1: TProgressBar;
        procedure Timer1Timer(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1:TForm1;
      i:integer;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Inc(i);
      if i>100 then i:=1;
      label1.caption:=IntToStr(i);
      ProgressBar1.Position:=ProgressBar1.Position+1;//進度條
      if i=100 then Timer1.Enabled:=False;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not Timer1.Enabled then
      begin
        i:=0;
        ProgressBar1.Min:=0;
        ProgressBar1.Max:=100;
        Application.ProcessMessages;
        Timer1.Interval:=100;
        Timer1.Enabled:=true;
      end;
    end;end.
      

  8.   

    加一句话就行了“label1.Repaint;”for m:= 1 to 100 do
    begin
      ss:=Format('%.6d',[m]);
      leep(50);
      label1.caption:=Format('%.6d',[m]);
      label1.Repaint;
    end;
      

  9.   

    for m:= 1 to 100 do
    begin
    application.process//好像是这么写的吧
    ss:=Format('%.6d',[m]);
    sleep(50);
      label1.caption:=ss;end;