views:

102

answers:

3

I'm new to component development in Delphi, therefore want to know, is it possible to implement my task at all.

I need to create a visual component (user control) based on TScrollBox, which will represent a bunch of TPanel, all that panels will be aligned as "Top" inside that TScrollBox and can have different Height. It has to act as TCollection (add, delete. reorder), and must allow users to add other controls into these panels at designtime.

I've created these classes for component:

type
  TPanelsGrid = class;

  TPanelsGridItem = class(TCollectionItem)
  private
    FPanel: TPanel;
    procedure SetPanel(Value: TPanel);
    function GetGrid: TPanelsGrid;
  protected
    function GetDisplayName: string; override;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    // This is my TPanel object that should be used at designtime
    // I thought "stored True" will serialize it automatically but I was wrong
    property Panel: TPanel read FPanel write SetPanel stored True; 
  end;

  TPanelsGridItems = class(TCollection)
  private
    FPanelsGrid: TPanelsGrid;
  protected
    function GetItem(Index: Integer): TPanelsGridItem;
    procedure SetItem(Index: Integer; Value: TPanelsGridItem);

    function GetOwner: TPersistent; override;
    procedure Update(Item: TCollectionItem); override;
  public
    property EditorsGrid: TPanelsGrid read FPanelsGrid;
    property Items[Index: Integer]: TPanelsGridItem
      read GetItem write SetItem; default;

    constructor Create(PanelsGrid: TPanelsGrid);
    function Add: TPanelsGridItem;
    procedure Delete(Index: Integer);
  end;

  TPanelsGrid = class(TScrollBox)
  private
    FItems: TPanelsGridItems;
    procedure SetItems(Value: TPanelsGridItems);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Items: TPanelsGridItems read FItems write SetItems;
  end;

This component is working ok at designtime, I can add-delete panels in stack, when I'm dropping some control (e.g. TCheckbox) on any panel, it's displayed as "owned by that panel": e.g. I can't drag this checkbox out of panel.

But this checkbox isn't stored in DFM-file and isn't displayed in "Structure" window.

I guess there must be some manual serialization-deserialization of TPanel's content, but I have no idea how to do that. Can't find any example on Internet. Plase give me some guideline, if such implementation is possible at all.

Addition:

This is how my DFM-file fragment looks like after adding 3 panels into grid:

  object PanelsGrid1 : TPanelsGrid 
    Left = 8
    Top = 8
    Width = 536
    Height = 382
    Anchors = [akLeft, akTop, akRight, akBottom]
    TabOrder = 0
    Items = <
      item
      end
      item
      end
      item
      end>
  end

As you can see, all items are empty but I dropped there a checkbox and radiobutton into item #3.

A: 

I think you can look at the TMS Poly List control

The TMS Advanced Poly List components offer an extremely versatile and flexible architecture to create virtually any possible lists of items in user interfaces. This is seen typically but not limited to the new Office 2010 application menu. Contrary to most user interface list controls, where a list consists of items of the same type or a collection of items of the same type, the TMS Advanced Poly List components can hold polymorph items. All items just need to descend from the base class TCustomItem and any inherited items can be added. TMS Advanced Poly List components come with a large set of prebuilt list items but custom item classes can be added by either descending of the TCustomItem base class or any of the classes already provided. There are item classes to show as list section item, text item with HTML formatting, text item with buttons, item with expand/collaps behaviour, item with image and many more. Items can be added in the polymorph lists either at design time, with a rich design time editor and at runtime via code.

Hugues Van Landeghem
Thanks. Too bad that it has closed source code. But if I will not find the solution in my own code, sure will think about purchasing.
Andrew
A: 

Make sure your child panels have names. You can override TCollection.Notify and if Action is cnAdded, make sure the panel has an name.

Cosmin Prund
+1  A: 

After all I decided to give up using TCollection, because during testing of DefineProperties method I has consistent IDE crash. I think TCollection just wasn't designed for such task.

I founded an appropriate implementation inside Delphi sources inside of control ExtCtrls.TCustomCategoryPanelGroup. It maintains the stack of panels which can be added or removed both at design time and runtime. I created my own classes, using the source code of TCustomCategoryPanelGroup and TCustomCategoryPanel and it works as I want.

Andrew