Delphi 父窗体按钮

发布于 2024-08-17 16:38:20 字数 135 浏览 3 评论 0原文

有没有办法禁用父窗口按钮?我有一个被很多表单调用的“工作”表单,我想禁用父表单按钮,直到它完成它的工作。然后重新打开它们。

我想做一些附加到 OnShow 事件和 onClose 事件的事情。

谢谢

-布拉德

Is there a way to disable the parent windows buttons? I have a "working" form that is called by a lot of forms, that I would like to disable the parent form buttons until it's finished doing it's thing. Then turn them back on.

I'd like to do something that is attached to the OnShow event and onClose event.

Thanks

-Brad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

憧憬巴黎街头的黎明 2024-08-24 16:38:20

创建您想要调用的表单,如下所示:

  unit fMyModalForm;
  interface
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs;
  type
    TfrmMyModalForm = class(TForm)
      procedure FormShow(Sender: TObject);
      procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
      fCallingForm: TForm;
      { Private declarations }
    public
      { Public declarations }
       property CallingForm: TForm read fCallingForm write fCallingForm;
    end;
  (*
  var
    frmMyModalForm: TfrmMyModalForm;
  *)
  implementation

  {$R *.dfm}

  procedure TfrmMyModalForm.FormShow(Sender: TObject);
  begin
     fCallingForm.Enabled := False;
  end;

  procedure TfrmMyModalForm.FormClose(Sender: TObject;
    var Action: TCloseAction);
  begin
     fCallingForm.Enabled := True;
  end;

  end.

然后在您想要调用此模态表单的按钮之后:

  unit fMain;

  interface

  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls,
    fMyModalForm;

  type
    TfrmMain = class(TForm)
      btnCall: TButton;
      btn1: TButton;
      btn2: TButton;
      procedure btnCallClick(Sender: TObject);
    private
      { Private declarations }
        f : TfrmMyModalForm;
    public
      { Public declarations }
    end;

  var
    frmMain: TfrmMain;

  implementation


  {$R *.dfm}

  procedure TfrmMain.btnCallClick(Sender: TObject);
  begin
     if not Assigned(f)
     then begin
        f := TfrmMyModalForm.Create(Self);
        f.CallingForm := Self;
     end;
     f.Show();
  end;

  end.

如果您只想禁用所有按钮,您可以迭代它们,而不是禁用 CallingForm,仅禁用调用表单。请参阅 Stack Overflow 主题(以及我的答案):动态转换表单编辑:或查看 _J_ 的答案(基本上显示了主题)。

不过,我会使用操作而不是按钮。

Create the form you want to call, as in:

  unit fMyModalForm;
  interface
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs;
  type
    TfrmMyModalForm = class(TForm)
      procedure FormShow(Sender: TObject);
      procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
      fCallingForm: TForm;
      { Private declarations }
    public
      { Public declarations }
       property CallingForm: TForm read fCallingForm write fCallingForm;
    end;
  (*
  var
    frmMyModalForm: TfrmMyModalForm;
  *)
  implementation

  {$R *.dfm}

  procedure TfrmMyModalForm.FormShow(Sender: TObject);
  begin
     fCallingForm.Enabled := False;
  end;

  procedure TfrmMyModalForm.FormClose(Sender: TObject;
    var Action: TCloseAction);
  begin
     fCallingForm.Enabled := True;
  end;

  end.

Then after the button where you want to call this modal form:

  unit fMain;

  interface

  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls,
    fMyModalForm;

  type
    TfrmMain = class(TForm)
      btnCall: TButton;
      btn1: TButton;
      btn2: TButton;
      procedure btnCallClick(Sender: TObject);
    private
      { Private declarations }
        f : TfrmMyModalForm;
    public
      { Public declarations }
    end;

  var
    frmMain: TfrmMain;

  implementation


  {$R *.dfm}

  procedure TfrmMain.btnCallClick(Sender: TObject);
  begin
     if not Assigned(f)
     then begin
        f := TfrmMyModalForm.Create(Self);
        f.CallingForm := Self;
     end;
     f.Show();
  end;

  end.

If you just want to disable all buttons you can iterate through them and in stead of disabling the CallingForm only disable the buttons on the CallingForm. See the Stack Overflow topic (and my answer) at :Cast a form dynamically EDITED: or see answer of _J_ (which basically show the topic).

I would use actions in stead of buttons though.

喜爱皱眉﹌ 2024-08-24 16:38:20

如果辅助窗口打开,执行某些操作并关闭,那么使用 ShowModal 而不是 Show 打开它是有意义的,这样用户就无法使用主窗体直到第二张表格关闭。

如果您想遍历所有按钮并禁用或启用它们,代码将如下所示:

var
  i: Integer;
begin
  for i := 0 to MainForm.ComponentCount - 1 do
    if (MainForm.Components[i] is TButton) then
      TButton(MainForm.Components[i]).Enabled := False;
end;

If the secondary window opens, does something and closes, then it would make sense to open it with ShowModal instead of Show, that way the user can't use the main form until the second form has closed.

If you want to iterate though all the buttons and disable or enable them, the code would look something like this:

var
  i: Integer;
begin
  for i := 0 to MainForm.ComponentCount - 1 do
    if (MainForm.Components[i] is TButton) then
      TButton(MainForm.Components[i]).Enabled := False;
end;
滥情稳全场 2024-08-24 16:38:20

对于这样的东西,您只需要 1 行代码和一个 TActionList 组件。

创建一个包含操作的操作列表,并将该操作链接到按钮。操作具有 OnUpdate 事件,可让您确定是否应启用该操作(以及链接的按钮)。每次操作应该知道是否必须启用它时,都会触发 OnUpdate 事件。

For stuf like this you will need only 1 line of code and an TActionList component.

Create an actionlist with an action and link the action to the button. An action has an OnUpdate event which lets you determine if the action (and thus the linked button) should be enabled. The OnUpdate event is triggered everytime the action should know if it must be enabled or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文