我有一个包含按钮和标签的程序,我希望标签能够在0到100000之间快速计算数字。
我试过了System.Diagnostics stopwatch
,但它不是我想要的。
procedure TForm1.Button1Click(Sender: TObject);
var
sw: TStopwatch;
begin
sw := TStopwatch.StartNew;
Timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
sw: TStopwatch;
begin
sw.Start;
label1.Caption := IntToStr(sw.ElapsedMilliseconds);
end;
答案 0 :(得分:1)
TStopWatch
(您没有正确使用,BTW)不是您正在寻找的那种计数器。您需要一个简单的Integer
变量,例如:
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
Counter: Integer;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Counter := 0;
Label1.Caption := IntToStr(Counter);
Timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Inc(Counter);
Label1.Caption := IntToStr(Counter);
if Counter = 100000 then Timer1.Enabled := False;
end;