- How can I disable all components in a non-modal form?
- procedure TMyForm.OnMyAction(Sender: TObject);
- begin
- try
- // notify Action Manager that the Action is temporarily disabled
- SomeGlobalFlag := True;
- // disable the action
- (Sender as TAction).Enabled := False;
- // do the call
- ShellExecAndWait( ... );
- finally
- // enable the action
- (Sender as TAction).Enabled := True;
- // allow ActionManager to control the action again
- SomeGlobalFlag := False;
- end;
- end;
- procedure EnableControls(Parent: TWinControl; Enabled: Boolean);
- var
- i: Integer;
- Ctl: TControl;
- begin
- for i := 0 to Pred(Parent.ControlCount) do begin
- Ctl := Parent.Controls[i];
- Ctl.Enabled := Enabled;
- if Ctl is TWinControl then
- EnableControls(TWinControl(Ctl), Enabled);
- end;
- end;
- procedure TMyForm.OnMyAction(Sender: TObject);
- begin
- EnableControls(Self, False);
- try
- ShellExecAndWait(...);
- finally
- EnableControls(Self, True);
- end;
- end;
- var
- ActionIsExecuting: Boolean = False;
- procedure TMyForm.OnMyAction(Sender: TObject);
- begin
- // notify Action Manager that the Action is temporarily disabled
- ActionIsExecuting := True;
- try
- // do the call
- ShellExecAndWait( ... );
- finally
- // allow ActionManager to control the action again
- ActionIsExecuting := False;
- end;
- end;
- procedure TSomeModule.ActionUpdate(Sender: TObject);
- begin
- (Sender as TAction).Enabled := not ActionIsExecuting and ...
- end;
- procedure TMyForm.OnMyAction(Sender: TObject);
- {$J+} // a.k.a. $WRITABLECONST ON
- const
- ActionIsExecuting: Boolean = False;
- {$J-}
- begin
- if ActionIsExecuting then begin
- ShowMessage('The program is still running. Please wait.');
- exit;
- end;
- ActionIsExecuting := True;
- try
- ShellExecAndWait(...);
- finally
- ActionIsExecuting := False;
- end;
- end;
- type
- TTemporaryFlag = class(TInterfacedObject)
- private
- FFlag: PBoolean;
- public
- constructor Create(Flag: PBoolean);
- destructor Destroy; override;
- end;
- function TemporaryFlag(Flag: PBoolean): IUnknown;
- begin
- Result := TTemporaryFlag.Create(FFlag);
- end;
- constructor TTemporaryFlag.Create;
- begin
- inherited;
- FFlag := Flag;
- FFlag^ := True;
- end;
- destructor TTemporaryFlag.Destroy;
- begin
- FFlag^ := False;
- inherited;
- end;
- begin
- TemporaryFlag(@ActionIsExecuting);
- ShellExecAndWait(...);
- end;
- procedure TMyForm.OnMyAction(Sender: TObject);
- begin
- try
- // disable all actions
- (Sender as TAction).ActionList.State := asSuspended;
- // do the call
- ShellExecAndWait( ... );
- finally
- // enable all actions
- (Sender as TAction).ActionList.State := asNormal;
- end;
- end;