如何计算 delphi 中标签中的数字
How to count up numbers in a label in delphi
我有一个包含按钮和标签的程序,例如,我希望标签从 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;
TStopWatch
(顺便说一句,您使用不正确)不是您要找的那种计数器。您需要一个简单的 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;
我有一个包含按钮和标签的程序,例如,我希望标签从 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;
TStopWatch
(顺便说一句,您使用不正确)不是您要找的那种计数器。您需要一个简单的 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;