views:

396

answers:

3

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

+1  A: 

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.

Edelcom
A: 

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;
_J_
I don't want it opened as a ShowModal because there are other processes going on in other forms in the background, and ShowModal stops those processes as far as I am aware of (I'm a novice Delphi programmer at best)
Brad
Yes, you are right, I wasn't aware of that fact.
_J_
A: 

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.

birger