如何根据Delphi中的某些变量禁用和启用Button Click事件?

时间:2018-05-07 15:11:25

标签: delphi delphi-10-seattle

我有一个Delphi 10.0 Seattle项目将一些文件FTP到服务器。

我将执行以下操作:

  1. 在Button1 OnClick事件中,我将计算一些内容并生成一个文本文件。 Edit1将保存文件名。

  2. 在Button2 OnClick事件中,将删除文件名的最后4个字符。该文件将使用新名称重命名。 Edit2将保留新名称,并将其上载到一台服务器。

  3. 在文件上传过程中,没有按钮点击工作,无法关闭表单。我使用了一个布尔变量FileToBeTranferred。表单创建时false

  4. 我写了以下代码:

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;
    
    type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    var
      FileToBeTranferred: boolean;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if FileToBeTranferred = false then
        begin
          //Do some calculation
          //Edit1.Text := Output File Name
          FileToBeTranferred := true;
        end
      else
        begin
          Button1.Click(nil);
        end;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if FileToBeTranferred = true then
      begin
        Edit2.Text := delete(Edit1.Text, (length(Edit1.Text)-4), 4);
        //Upload to Server
        Button2.Click(self);
      end
      else
      begin
        //Upload finished
        FileToBeTranferred := false;
        Button2.Click(nil);
      end;
    end;
    
    procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
     if FileToBeTranferred = true then CanClose := false
     else CanClose := true;
    
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FileToBeTranferred := false;
    end;
    
    end.
    

    我无能为力。我收到太多错误。错误如下:

      [dcc32 Error] Unit1.pas(44): E2197 Constant object cannot be passed as var parameter
    1. Edit2.Text := delete(Edit1.Text, (length(Edit1.Text)-4), 4); - 用于删除文件重命名的最后4个字符。

    2. [dcc32 Error] Unit1.pas(47): E2034 Too many actual parameters
    3. Button2.Click(nil); - 由于FTP正在进行中,因此不允许对文件进行更多修改。 Button1.Click不会执行任何操作。

    4. [dcc32 Error] Unit1.pas(47): E2034 Too many actual parameters
    5. Button2.Click(self); - 当FTP进度完成时,文件已准备好附加。 Button2.Click将无法正常工作。

    6. 解决方法是什么?

1 个答案:

答案 0 :(得分:2)

您展示的代码只是各种错误。不仅是语法错误,还有逻辑错误。即使编译了代码,如果Button1为真,你的FileToBeTranferred点击处理程序也会陷入无休止的递归循环,而你的Button2点击处理程序将陷入无休止的递归循环,无论FileToBeTranferred

尝试更像这样的东西:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    FileToBeTranferred: boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not FileToBeTranferred then
  begin
    //Do some calculation
    //Edit1.Text := Output File Name
    FileToBeTranferred := True;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if FileToBeTranferred then
  begin
    Edit2.Text := Copy(Edit1.Text, 1, Edit1.GetTextLen-4);
    //Upload to Server, wait for completion
    FileToBeTranferred := False;
  end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := not FileToBeTranferred;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FileToBeTranferred := False;
end;

end.

话虽如此,您可能会考虑采用不同的方法。例如,根本不需要FileToBeTranferred布尔值的那个:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled := False;
  //Do some calculation
  //Edit1.Text := Output File Name
  Button2.Enabled := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Button2.Enabled := False;
  Edit2.Text := Copy(Edit1.Text, 1, Edit1.GetTextLen-4);
  //Upload to Server
  Button1.Enabled := True;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := Button1.Enabled;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Enabled := True;
  Button2.Enabled := False;
end;

end.

或者,您可以将这两种方法结合起来,方法是在任何给定时刻根据TButton的当前值启用/禁用FileToBeTranferred个对象。

您可以覆盖表单的虚拟UpdateActions()方法:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  protected
    procedure UpdateActions; override;
  private
    { Private declarations }
    FileToBeTranferred: boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  //Do some calculation
  //Edit1.Text := Output File Name
  FileToBeTranferred := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit2.Text := Copy(Edit1.Text, 1, Edit1.GetTextLen-4);
  //Upload to Server, wait for completion
  FileToBeTranferred := False;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := not FileToBeTranferred;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FileToBeTranferred := False;
end;

procedure TForm1.UpdateActions;
begin
  inherited;
  Button1.Enabled := not FileToBeTranferred;
  Button2.Enabled := FileToBeTranferred;
end;

end.

或者,您可以在表单上放置TActionList并为每个TAction分配TButton,然后在{{1}中启用/禁用TAction个对象事件:

OnUpdate