Recent

Author Topic: [Solved] A component that contains another  (Read 2721 times)

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
[Solved] A component that contains another
« on: January 03, 2017, 11:59:47 pm »
Hi, I'm working on a component that's like this:

- Panel
-- Listbox

A panel containing a listbox. The problem I have is if I add in 'published' the ListBox property and I change it with the object inspector then when I run the program the properties (say for example Items property) is not loaded at all.

I create the ListBox on create event of the panel container.

The source code is here:
https://github.com/bgrabitmap/bgracontrols/blob/master/bclistbox.pas

In the source I not publish the property because it doesn't works and well but is usable at least without it.
« Last Edit: January 04, 2017, 02:34:35 pm by lainz »

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: A component that contains another
« Reply #1 on: January 04, 2017, 04:55:22 am »
Just a guess (a long time since I did it)
Use SetSubComponent for the listbox in the register proc and a Propertyeditor for it?

Example here: http://wiki.freepascal.org/Poweredby#Other_uses
« Last Edit: January 04, 2017, 05:02:32 am by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: A component that contains another
« Reply #2 on: January 04, 2017, 05:42:39 am »
Thanks for your reply, is too late here I will try tomorrow.

balazsszekely

  • Guest
Re: A component that contains another
« Reply #3 on: January 04, 2017, 07:18:45 am »
Hi @lainz,

Only publish the properties you need, something like this:
Code: Pascal  [Select][+][-]
  1. //...
  2. type
  3.  TBCPaperListBox = class(TBCPaperPanel)
  4.   private
  5.     FListBox: TBCListBox;
  6.     function GetListBoxItems: TStrings;
  7.     procedure SetListBoxItems(AItems: TStrings);
  8.   public
  9.     constructor Create(TheOwner: TComponent); override;
  10.   published
  11.     property Items: TStrings read GetListBoxItems write SetListBoxItems;
  12.   end;        
  13.  
  14. //...
  15. { TBCPaperListBox }
  16.  
  17. constructor TBCPaperListBox.Create(TheOwner: TComponent);
  18. begin
  19.   inherited Create(TheOwner);
  20.   Self.ChildSizing.ControlsPerLine := 1;
  21.   Self.ChildSizing.LeftRightSPacing := 4;
  22.   Self.ChildSizing.TopBottomSpacing := 5;
  23.   FListBox := TBCListBox.Create(Self);
  24.   FListBox.Align := alClient;
  25.   FListBox.Parent := Self;
  26. end;
  27.  
  28. function TBCPaperListBox.GetListBoxItems: TStrings;
  29. begin
  30.   Result := FListBox.Items;
  31. end;
  32.  
  33. procedure TBCPaperListBox.SetListBoxItems(AItems: TStrings);
  34. begin
  35.   if FListBox.Items <> AItems then
  36.     FListBox.Items := AItems;
  37. end;

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: A component that contains another
« Reply #4 on: January 04, 2017, 02:26:06 pm »
Oh I see. That has some kind of advantages, for example that the user for error clears the ListBox assigned in the property I publish, with your way it will not happen.

Edit: Well tested the minesadorada code and works fine. To avoid the user to clear the property we must register the property editor for the class, as displayed in the wiki.
« Last Edit: January 04, 2017, 02:34:23 pm by lainz »

 

TinyPortal © 2005-2018