请教delphi用一个按钮实现启动和暂停(或者是显示/隐藏)?
  这个按钮点一下就是启动,再点一下就是暂停,再点一次的话又是启动.....目的是一个按钮实现两种结果。

解决方案 »

  1.   

    可以在按钮的过程里设一个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是第一次点击时出现的动作。——本人才疏学浅,程序算法难免不是优秀,希望大家谅解。:)
      

  2.   


    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.
      

  3.   

    启动暂停各是一个方法,然后用一个属性知识状态。
    最后在Button的事件里判断调用。
      

  4.   

    随便加个标志就可以了吧  一个BOOLEAN标志足够了啊 。给点分吧 穷死了啊
      

  5.   

    每一个控件都有一个tag属性,可以用来添加标志,然后根据这个标志判断就可以了。
      

  6.   

    (1)設置一個標誌,初始化標誌第一狀態值.
    (2)判斷執行,執行完(第一/第二)個狀態後,給標誌變量賦(第二/第一)狀態值.
    (3)可以用boolean變量作為標誌判斷;
       也可以用控件的tag屬性賦值判斷.
      

  7.   

    if button1.caption='启动' then
    begin
    ......
    button1.caption:='暂停';
    end
    else
    begin
    ......
    button1.caption:='启动';
    end;
      

  8.   


    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. 
      

  9.   

    另外的一种实现方法
    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.
      

  10.   

    实在是SORRY,我写完贴才看见2楼的兄弟写出来来了,和我是样的道理,
    我写的那个代码,存在逻辑错误,比如,点击第一次时,应该显示暂停,而我却相反.
    第二,在声明B变量时没有给初始化值(虽然他默认值是TRUE,但是,代码可读性差).
    向2楼的学习,代码风格很棒
      

  11.   

    个人还是习惯用Button1的Tag属性来判定,不习惯用全局变量
      

  12.   

    if Button1.Tag = 0 then begin
      //Do Something
      Button1.Tag := 1;
    end
    else if Button1.Tag = 1 then begin
      //Do Something
      Button1.Tag := 0;
    end;
      

  13.   

    用tag判断 既方便又省事!
      

  14.   

    感谢pq1994,tianhuo_soft ,等各位朋友的回复,请斑竹给他们加分,我没有办法加,说我没有权限