可以在按钮的过程里设一个integer值,点击一次就加1,然后用mod判断是不是2的倍数,如果是2的倍数就启动,如果不是2的倍数就不启动。 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; a : integer = 0; implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin a:=a+1; if (a mod 2 = 0) then begin do something; end; if (a mod 2 = 1) then begin do something; end; end;end.但是注意,a mod = 1是第一次点击时出现的动作。——本人才疏学浅,程序算法难免不是优秀,希望大家谅解。:)
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; bStaut:boolean=true;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin if bStaut=true then begin Button1.Caption:='停止'; bStaut:=false; end else begin Button1.Caption:='启动'; bStaut:=true; end;end;procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption:='启动'; end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
a : integer = 0;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
a:=a+1; if (a mod 2 = 0) then
begin
do something;
end; if (a mod 2 = 1) then
begin
do something;
end;
end;end.但是注意,a mod = 1是第一次点击时出现的动作。——本人才疏学浅,程序算法难免不是优秀,希望大家谅解。:)
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
bStaut:boolean=true;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if bStaut=true then
begin
Button1.Caption:='停止';
bStaut:=false;
end
else
begin
Button1.Caption:='启动';
bStaut:=true;
end;end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='启动';
end;end.
最后在Button的事件里判断调用。
(2)判斷執行,執行完(第一/第二)個狀態後,給標誌變量賦(第二/第一)狀態值.
(3)可以用boolean變量作為標誌判斷;
也可以用控件的tag屬性賦值判斷.
begin
......
button1.caption:='暂停';
end
else
begin
......
button1.caption:='启动';
end;
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
isStop:boolean;
public
{ Public declarations }
end; var
Form1: TForm1;
a : integer = 0;
implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
begin
isStop:=not isStop;
if isStop then
Stop
else
start;
end; end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
b:boolean;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
begin
if b then begin
Button1.Caption:='开始';
//写你开始执行代码
b:=false;
end
else begin
Button1.Caption:='暂停';
//写暂停要执行的代码
b:=True;
end;
end;end;end.
我写的那个代码,存在逻辑错误,比如,点击第一次时,应该显示暂停,而我却相反.
第二,在声明B变量时没有给初始化值(虽然他默认值是TRUE,但是,代码可读性差).
向2楼的学习,代码风格很棒
//Do Something
Button1.Tag := 1;
end
else if Button1.Tag = 1 then begin
//Do Something
Button1.Tag := 0;
end;