Recent

Author Topic: [SOLVED] Add property to component  (Read 405 times)

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
[SOLVED] Add property to component
« on: October 19, 2020, 03:29:43 pm »
Hi All,

I am making a simple component based on TCustomPanel.

Code: Pascal  [Select][+][-]
  1. unit
  2. MyPanel;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  10.  
  11. type
  12.   TMyPanel = class(TCustomPanel)
  13.   private
  14.  
  15.   protected
  16.  
  17.   public
  18.     constructor Create(AOwner : TComponent); override;
  19.   published
  20.     property Align;
  21.     property Canvas;
  22.   end;
  23.  
  24. procedure Register;
  25.  
  26. implementation
  27.  
  28. constructor TMyPanel.Create(AOwner : TComponent);
  29. begin
  30.   inherited Create(AOwner);
  31.   Color := clWhite;
  32.   BevelOuter := bvNone;
  33. end;
  34.  
  35. procedure Register;
  36. begin
  37.   RegisterComponents('Misc',[TMyPanel]);
  38. end;
  39.  
  40. end.
  41.  

This works but I want to add a new property based on

type
  TBorders = record
    bTop : boolean;
    bLeft : boolean;
    bRight : boolean;
    bBottom : boolean;
  end;

How do I do this?

Thanks in advance.
« Last Edit: October 19, 2020, 05:35:31 pm by pcurtis »
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Add propety to component
« Reply #1 on: October 19, 2020, 03:47:36 pm »
you are looking for a Set property

Create a Enum type with all those in it first the create your set..

Type
  TMyEnum =(bleft,btop,bright,bbottom);

  TMySet = Set of MyEnum;

The class code;
fMyset:TmySet;
Published
  property TheSets :TMySet; read FMySet write fMyset;


this is just an idea ..

The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Add propety to component
« Reply #2 on: October 19, 2020, 03:56:12 pm »
Note: record cannot be published (can be public).
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Add propety to component
« Reply #3 on: October 19, 2020, 04:13:34 pm »
OK. I changed the code to

Code: Pascal  [Select][+][-]
  1. unit
  2. MyPanel;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  10. type
  11.   TMyEnum =(bleft,btop,bright,bbottom);
  12.  
  13.   TMySet = Set of TMyEnum;
  14.  
  15. type
  16.   TMyPanel = class(TCustomPanel)
  17.   private
  18.     fMyset:TmySet;
  19.   protected
  20.  
  21.   public
  22.     constructor Create(AOwner : TComponent); override;
  23.     property TheSets : TMySet read fMySet write fMyset;
  24.   published
  25.     property Align;
  26.     property Canvas;
  27.   end;
  28.  
  29. procedure Register;
  30.  
  31. implementation
  32.  
  33. constructor TMyPanel.Create(AOwner : TComponent);
  34. begin
  35.   inherited Create(AOwner);
  36.   Color := clWhite;
  37.   BevelOuter := bvNone;
  38. end;
  39.  
  40. procedure Register;
  41. begin
  42.   RegisterComponents('Misc',[TMyPanel]);
  43. end;
  44.  
  45. end.
  46.  

It compiles and installs but does not appear in the object inspector.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Add propety to component
« Reply #4 on: October 19, 2020, 04:27:35 pm »
it needs to be in the published section..

that is what published is all about among other things.
The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Add propety to component
« Reply #5 on: October 19, 2020, 04:30:22 pm »
See comment from Blaazen

Quote
Note: record cannot be published (can be public).
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Add property to component
« Reply #6 on: October 19, 2020, 04:31:34 pm »
that isn't a record...

a set is not a record.

please try it.
The only true wisdom is knowing you know nothing

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Add property to component
« Reply #7 on: October 19, 2020, 04:42:41 pm »
Here's the code. It doesn't work.

Code: Pascal  [Select][+][-]
  1. unit
  2. MyPanel;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  10. type
  11.   TMyEnum =(bleft,btop,bright,bbottom);
  12.  
  13.   TMySet = Set of TMyEnum;
  14.  
  15. type
  16.   TMyPanel = class(TCustomPanel)
  17.   private
  18.     fMyset:TmySet;
  19.   protected
  20.  
  21.   public
  22.     constructor Create(AOwner : TComponent); override;
  23.   published
  24.     property Align;
  25.     property Canvas;
  26.     property TheSets : TMySet read fMySet write fMyset;
  27.   end;
  28.  
  29. procedure Register;
  30.  
  31. implementation
  32.  
  33. constructor TMyPanel.Create(AOwner : TComponent);
  34. begin
  35.   inherited Create(AOwner);
  36.   Color := clWhite;
  37.   BevelOuter := bvNone;
  38. end;
  39.  
  40. procedure Register;
  41. begin
  42.   RegisterComponents('Misc',[TMyPanel]);
  43. end;
  44.  
  45. end.
  46.  
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Handoko

  • Hero Member
  • *****
  • Posts: 3913
  • My goal: build my own game engine using Lazarus
Re: Add property to component
« Reply #8 on: October 19, 2020, 05:11:38 pm »

pcurtis

  • Sr. Member
  • ****
  • Posts: 351
Re: Add property to component
« Reply #9 on: October 19, 2020, 05:34:37 pm »
Handoko - Thanks.

Code: Pascal  [Select][+][-]
  1. unit
  2. MyPanel;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  10. type
  11.   TMyEnum =(bleft,btop,bright,bbottom);
  12.  
  13.   TMySet = Set of TMyEnum;
  14.  
  15. type
  16.   TMyPanel = class(TCustomPanel)
  17.   private
  18.     fMyset:TmySet;
  19.     function GetMySet : TMySet;
  20.     procedure SetMySet(AValue : TMySet);
  21.   protected
  22.  
  23.   public
  24.     constructor Create(AOwner : TComponent); override;
  25.  
  26.   published
  27.     property TheSets : TMySet read GetMySet write SetMyset;
  28.     property Align;
  29.     property Canvas;
  30.   end;
  31.  
  32. procedure Register;
  33.  
  34. implementation
  35.  
  36. constructor TMyPanel.Create(AOwner : TComponent);
  37. begin
  38.   inherited Create(AOwner);
  39.   Color := clWhite;
  40.   BevelOuter := bvNone;
  41. end;
  42.  
  43. function TMyPanel.GetMySet : TMySet;
  44. begin
  45.   result := fMyset;
  46. end;
  47.  
  48. procedure TMyPanel.SetMySet(AValue : TMySet);
  49. begin
  50.   if fMyset <> AValue then
  51.     begin
  52.       fMyset := AValue;
  53.     end;
  54. end;
  55.  
  56. procedure Register;
  57. begin
  58.   RegisterComponents('Misc',[TMyPanel]);
  59. end;
  60.  
  61. end.
  62.  
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Add property to component
« Reply #10 on: October 19, 2020, 05:37:33 pm »
Basically, properties don't have to have setter. But when the property changes there will be no notification. Setter usually triggers at least Invalidate or something more sophisticated.
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018