Lazarus

Programming => General => Topic started by: pcurtis on May 10, 2021, 06:30:03 pm

Title: [SOLVED] Hide property
Post by: pcurtis on May 10, 2021, 06:30:03 pm
With the following code I can make a new Timer component.

How can I hide the Enabled property in the object inspector?

Code: Pascal  [Select][+][-]
  1. unit MyTimer;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.   TMyTimer = class(TTimer)
  12.   private
  13.     fOneShot : boolean;
  14.     function GetOneShot : boolean;
  15.     procedure SetOneShot(aValue : boolean);
  16.   protected
  17.  
  18.   public
  19.     constructor Create(AOwner : TComponent); override;
  20.     procedure Reset;
  21.     procedure Start;
  22.     procedure Stop;
  23.   published
  24.     procedure DoOnTimer; override;
  25.     property OneShot : boolean read GetOneShot write SetOneShot;
  26.   end;
  27.  
  28. procedure Register;
  29.  
  30. implementation
  31.  
  32. {$R MyTimer.res}
  33.  
  34. constructor TMyTimer.Create(AOwner : TComponent);
  35. begin
  36.   Enabled := False;
  37.   fOneShot := False;
  38.   inherited Create(AOwner);
  39. end;
  40.  
  41. procedure TMyTimer.DoOnTimer;
  42. begin
  43.   inherited;
  44.   if fOneShot then Self.Enabled := False;
  45. end;
  46.  
  47. procedure TMyTimer.Start;
  48. begin
  49.   Self.Enabled := True;
  50. end;
  51.  
  52. procedure TMyTimer.Reset;
  53. begin
  54.   Self.Enabled := False;
  55.   Self.Enabled := True;
  56. end;
  57.  
  58. procedure TMyTimer.Stop;
  59. begin
  60.   Self.Enabled := False;
  61. end;
  62.  
  63. function TMyTimer.GetOneShot : boolean;
  64. begin
  65.   Result := fOneShot;
  66. end;
  67.  
  68. procedure TMyTimer.SetOneShot(aValue : boolean);
  69. begin
  70.   If GetOneShot <> aValue then
  71.     begin
  72.       fOneShot := aValue;
  73.     end;
  74. end;
  75.  
  76. procedure Register;
  77. begin
  78.   RegisterComponents('Misc',[TMyTimer]);
  79. end;
  80.  
  81. end.
  82.  
Title: Re: Hide property
Post by: Mr.Madguy on May 10, 2021, 06:38:28 pm
Try this:
1) Declare the same property in another section. For example:
Code: Pascal  [Select][+][-]
  1. TMyTimer = class(TTimer)
  2. public
  3.   property Enabled;
  4. end;
  5.  
2) Inherit from TCustomTimer (if it exists)
Title: Re: Hide property
Post by: Remy Lebeau on May 10, 2021, 07:03:44 pm
How can I hide the Enabled property in the object inspector?

See Hide Properties (UnlistPublishedProperty)  (https://forum.lazarus.freepascal.org/index.php?topic=30518.0)
Title: Re: Hide property
Post by: pcurtis on May 10, 2021, 07:11:51 pm
@Mr.Madguy doesn't work
@Remy Lebeau Means nothing to me  :(
Title: Re: Hide property
Post by: Mr.Madguy on May 10, 2021, 08:26:02 pm
@Mr.Madguy doesn't work
At least 2nd one should work (1st one is easier, but isn't guaranteed to work), because it's real reason, why we need TCustom<xxx> controls. See how TTimer is declared and do the same:
Code: Pascal  [Select][+][-]
  1. uses CustomTimer;
  2.  
  3. type
  4.   TMyTimer = class (TCustomTimer)
  5.   published
  6.     //property Enabled;
  7.     property Interval;
  8.     property OnTimer;
  9.     property OnStartTimer;
  10.     property OnStopTimer;
  11.   end;  
  12.  
Title: Re: Hide property
Post by: pcurtis on May 10, 2021, 08:50:13 pm
@Mr.Madguy Job done. It works. Thanks.
Title: Re: Hide property
Post by: Remy Lebeau on May 11, 2021, 02:56:24 am
@Remy Lebeau Means nothing to me  :(

It means you can register the THiddenPropertyEditor class for the property you want to hide at design-time (it will still be available at runtime), eg:

Code: Pascal  [Select][+][-]
  1. unit MyTimer;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   ...
  9.      
  10. type
  11.   TMyTimer = class(TTimer)
  12.     ...
  13.   end;
  14.      
  15. procedure Register;
  16.      
  17. implementation
  18.      
  19. {$R MyTimer.res}
  20.  
  21. uses
  22.   PropEdits;
  23.      
  24. ...
  25.      
  26. procedure Register;
  27. begin
  28.   RegisterComponents('Misc', [TMyTimer]);
  29.   RegisterPropertyEditor(TypeInfo(Boolean), TMyTimer, 'Enabled', THiddenPropertyEditor);
  30. end;
  31.  
  32. end.

But, using the approach of deriving TMyTimer from TCustomTimer and NOT promoting the Enabled property to published is a better way to go.  It also hides the Enabled property at runtime, too.  I wasn't aware that FreePascal had TCustomTimer (Delphi doesn't).
Title: Re: Hide property
Post by: PascalDragon on May 11, 2021, 08:31:03 am
But, using the approach of deriving TMyTimer from TCustomTimer and NOT promoting the Enabled property to published is a better way to go.  It also hides the Enabled property at runtime, too.  I wasn't aware that FreePascal had TCustomTimer (Delphi doesn't).

It's more correct to say that Lazarus or the LCL has TCustomTimer, not Free Pascal. ;)
Title: Re: Hide property
Post by: Mr.Madguy on May 11, 2021, 10:28:13 am
But, using the approach of deriving TMyTimer from TCustomTimer and NOT promoting the Enabled property to published is a better way to go.  It also hides the Enabled property at runtime, too.  I wasn't aware that FreePascal had TCustomTimer (Delphi doesn't).
Can't check it now - will do it later, but I think, that any control should have TCustom analog both in Delphi and Lazarus.
TinyPortal © 2005-2018