运行如下代码,会发现当所有的Item增加完后,才会显示,
如何才能每增加一条就马上显示出来?
好像是用SendMessage,对吗?代码怎么写?
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to 100 do
  begin
    ListBox1.Items.Add(IntTostr(i));
    Sleep(50);
  end;
end;

解决方案 »

  1.   

    你有多少条要加啊
    去掉Sleep(50)后
    基本上就相当于瞬间显示了
      

  2.   

    ListBox1.Items.BeginUpdate;
    ListBox1.Items.Add(IntTostr(i));
    Sleep(50);
    ListBox1.Items.EndUpdate;
      

  3.   

    你需要创建一个线程来加item才能产生一个个显示出来,否则在buttonclick消息处理完毕前,主界面不会刷新。
      

  4.   

    no
    z这样可以的,不知道效率如何
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: integer;
    begin
      for i := 0 to 100 do
      begin
        ListBox1.Items.Add(IntTostr(i));
        SendMessage(ListBox1.Handle, WM_PAINT, 0, 0);
        Sleep(100);
      end;
    end;
      

  5.   

    procedure Delay(dwMilliseconds: Longint);
    var
      iStart, iStop: DWORD;
    begin
      iStart := GetTickCount;
      repeat
        iStop := GetTickCount;
        Application.ProcessMessages;
      until (iStop - iStart) >= dwMilliseconds;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i: integer;
    begin
      for i := 0 to 100 do
      begin
        ListBox1.Items.Add(IntTostr(i));
        Delay(10);
      end;
    end;做出来了,但不知道这样有什么意义呢
      

  6.   

    不如创建一个Timer,在OnTimer事件中加入ListBox1.Items.Add(IntTostr(i));
    如果用 sleep的话,程序会出现假死状态,很不爽的
    或者再建一个线程,那就随便了
    procedure TimerProc(window:HWND;msg,evid:uint;dwTime:DWORD);stdcall;
    beginend;
    procedure Thello.aa;
    begin
      ls.Items.Add(inttostr(n));
      inc(n);
      Application.ProcessMessages;
    end;procedure Thello.Execute;
    var
      TimerID:integer;
      Msg:TMsg;
    begin
      self.FreeOnTerminate:=true;
      TimerID:=SetTimer(0,0,1000,@TimerProc);
      while GetMessage(Msg,0,0,0) do
      begin
        windows.DispatchMessage(Msg);
        self.Synchronize(aa);
        if self.Terminated then
          break;
      end;
      KillTimer(0,TimerID);
      { Place thread code here }end;
      

  7.   

    暈啊...
    別人是要速度的,LZ竟然是要面子的!
    一般都是一次性算好其他東西在載入到 LISTBOX 裡面的,想不到LZ竟然要逆其道...
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: integer;
    begin
      for i := 0 to 100 do
      begin
        ListBox1.Items.Add(IntTostr(i));
        Sleep(100);
        ListBox1.Repaint; or ListBox1.Refresh;
      end;
    end;
      

  9.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    i: integer;
    begin
    for i := 0 to 100 do
    begin
    ListBox1.Items.Add(IntTostr(i));
    ListBox1.Selected[ListBox1.Count -1]:=True; //<---- Scroll to added item.
    application.processmessages;      //<-------- Add this to refresh screen.
    Sleep(50);
    end;
    end;
      

  10.   

    application.processmessages;
    !!!!!!!!!!!!!!!!!!!!关键