Recent

Author Topic: [SOLVED] Hide property  (Read 1172 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 634
[SOLVED] Hide property
« 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.  
« Last Edit: May 10, 2021, 08:51:01 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 667
Re: Hide property
« Reply #1 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)
30.04.2021 - DynamicData 4.0 is released and migration to it is completed.
It's more Lazarus-friendly, but still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 12 years old.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 933
    • Lebeau Software
Re: Hide property
« Reply #2 on: May 10, 2021, 07:03:44 pm »
How can I hide the Enabled property in the object inspector?

See Hide Properties (UnlistPublishedProperty)
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 634
Re: Hide property
« Reply #3 on: May 10, 2021, 07:11:51 pm »
@Mr.Madguy doesn't work
@Remy Lebeau Means nothing to me  :(
« Last Edit: May 10, 2021, 07:38:34 pm by pcurtis »
Windows 10
Laz 2.0.10
FPC 3.2.0

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 667
Re: Hide property
« Reply #4 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.  
« Last Edit: May 10, 2021, 08:28:03 pm by Mr.Madguy »
30.04.2021 - DynamicData 4.0 is released and migration to it is completed.
It's more Lazarus-friendly, but still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 12 years old.

pcurtis

  • Hero Member
  • *****
  • Posts: 634
Re: Hide property
« Reply #5 on: May 10, 2021, 08:50:13 pm »
@Mr.Madguy Job done. It works. Thanks.
Windows 10
Laz 2.0.10
FPC 3.2.0

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 933
    • Lebeau Software
Re: Hide property
« Reply #6 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).
« Last Edit: May 11, 2021, 02:58:39 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 3075
  • Compiler Developer
Re: Hide property
« Reply #7 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. ;)

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 667
Re: Hide property
« Reply #8 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.
30.04.2021 - DynamicData 4.0 is released and migration to it is completed.
It's more Lazarus-friendly, but still requires full Delphi 2009 support to be ported to Lazarus.
It's time to finally do it, because Delphi 2009 is 12 years old.

 

TinyPortal © 2005-2018