Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 2.64 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I disable all components in a non-modal form?
  2. procedure TMyForm.OnMyAction(Sender: TObject);
  3. begin
  4.   try  
  5.     // notify Action Manager that the Action is temporarily disabled
  6.     SomeGlobalFlag := True;
  7.  
  8.     // disable the action
  9.     (Sender as TAction).Enabled := False;
  10.  
  11.     // do the call
  12.     ShellExecAndWait( ... );
  13.  
  14.   finally
  15.  
  16.     // enable the action
  17.     (Sender as TAction).Enabled := True;
  18.  
  19.     // allow ActionManager to control the action again
  20.     SomeGlobalFlag := False;
  21.  
  22.   end;
  23. end;
  24.        
  25. procedure EnableControls(Parent: TWinControl; Enabled: Boolean);
  26. var
  27.   i: Integer;
  28.   Ctl: TControl;
  29. begin
  30.   for i := 0 to Pred(Parent.ControlCount) do begin
  31.     Ctl := Parent.Controls[i];
  32.     Ctl.Enabled := Enabled;
  33.     if Ctl is TWinControl then
  34.       EnableControls(TWinControl(Ctl), Enabled);
  35.   end;
  36. end;
  37.        
  38. procedure TMyForm.OnMyAction(Sender: TObject);
  39. begin
  40.   EnableControls(Self, False);
  41.   try
  42.     ShellExecAndWait(...);
  43.   finally
  44.     EnableControls(Self, True);
  45.   end;
  46. end;
  47.        
  48. var
  49.   ActionIsExecuting: Boolean = False;
  50.  
  51. procedure TMyForm.OnMyAction(Sender: TObject);
  52. begin
  53.   // notify Action Manager that the Action is temporarily disabled
  54.   ActionIsExecuting := True;
  55.   try  
  56.     // do the call
  57.     ShellExecAndWait( ... );
  58.   finally
  59.     // allow ActionManager to control the action again
  60.     ActionIsExecuting := False;
  61.   end;
  62. end;
  63.  
  64. procedure TSomeModule.ActionUpdate(Sender: TObject);
  65. begin
  66.   (Sender as TAction).Enabled := not ActionIsExecuting and ...
  67. end;
  68.        
  69. procedure TMyForm.OnMyAction(Sender: TObject);
  70. {$J+} // a.k.a. $WRITABLECONST ON
  71. const
  72.   ActionIsExecuting: Boolean = False;
  73. {$J-}
  74. begin
  75.   if ActionIsExecuting then begin
  76.     ShowMessage('The program is still running. Please wait.');
  77.     exit;
  78.   end;
  79.  
  80.   ActionIsExecuting := True;
  81.   try
  82.     ShellExecAndWait(...);
  83.   finally
  84.     ActionIsExecuting := False;
  85.   end;
  86. end;
  87.        
  88. type
  89.   TTemporaryFlag = class(TInterfacedObject)
  90.   private
  91.     FFlag: PBoolean;
  92.   public
  93.     constructor Create(Flag: PBoolean);
  94.     destructor Destroy; override;
  95.   end;
  96.  
  97. function TemporaryFlag(Flag: PBoolean): IUnknown;
  98. begin
  99.   Result := TTemporaryFlag.Create(FFlag);
  100. end;
  101.  
  102. constructor TTemporaryFlag.Create;
  103. begin
  104.   inherited;
  105.   FFlag := Flag;
  106.   FFlag^ := True;
  107. end;
  108.  
  109. destructor TTemporaryFlag.Destroy;
  110. begin
  111.   FFlag^ := False;
  112.   inherited;
  113. end;
  114.        
  115. begin
  116.   TemporaryFlag(@ActionIsExecuting);
  117.   ShellExecAndWait(...);
  118. end;
  119.        
  120. procedure TMyForm.OnMyAction(Sender: TObject);
  121. begin
  122.   try  
  123.     // disable all actions
  124.     (Sender as TAction).ActionList.State := asSuspended;
  125.  
  126.  
  127.     // do the call
  128.     ShellExecAndWait( ... );
  129.  
  130.   finally
  131.  
  132.     // enable all actions
  133.     (Sender as TAction).ActionList.State := asNormal;
  134.  
  135.   end;
  136. end;