2010-09-27 29 views
1

我是Delphi新組件開發,因此想知道,是否有可能實現我的任務。這可能嗎? TCollection後裔實現任意內容的TPanel容器的存儲

我需要創建一個基於TScrollBox的視覺組件(用戶控件),它將代表一堆TPanel,所有面板將在TScrollBox內對齊爲「頂部」,並且可以具有不同的高度。它必須充當TCollection(添加,刪除,重新排序),並且必須允許用戶在設計時將其他控件添加到這些面板中。

我創建這些類的成分:

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; 

該組件在設計時好的工作,我可以添加,刪除堆棧面板,當我在任何丟棄一些控制(如TCheckbox)面板顯示爲「由該面板擁有」:例如我無法將此複選框拖出面板。

但是這個複選框沒有存儲在DFM文件中,也沒有顯示在「結構」窗口中。

我想必須有一些TPanel的內容的手動序列化 - 反序列化,但我不知道該怎麼做。在Internet上找不到任何示例。 Plase給我一些指導,如果這樣的實現是可能的。

加成

這是我的DFM文件片段的樣子加入3片成網後:

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 

正如你可以看到,所有的項目都是空的,但是我放棄,但一複選框和單選按鈕到項目#3中。

+0

在設計時用鼠標右鍵單擊表單並選擇「作爲文本查看」。如果您不介意的話,找到與您的組件相關的部分並將其添加到您的帖子中。我不知道這應該如何實現,我自己,但讓我們先看看存儲的內容。 – himself 2010-09-27 16:28:13

+0

從TComponents繼承的已發佈屬性不會被流式傳輸系統存儲。 – 2010-09-27 17:59:22

+0

自己,做完了,請看dfm。 – Andrew 2010-09-27 18:46:24

回答

1

畢竟我決定放棄使用TCollection,因爲在測試DefineProperties方法時,我有一致的IDE崩潰。我認爲TCollection並不是爲這樣的任務而設計的。

我在Delphi控件ExtCtrls.TCustomCategoryPanelGroup內部創建了一個適當的實現。它維護可在設計時和運行時添加或刪除的面板堆棧。我創建了我自己的類,使用TCustomCategoryPanelGroup和TCustomCategoryPanel的源代碼,它可以按我的需要工作。

0

我覺得你可以看看TMS Poly List control

的TMS的先進聚組件列表 提供了一個非常靈活和 靈活的架構創建 幾乎在用戶界面項目 的任何可能的名單。這是典型的但不限於新的 Office 2010應用程序菜單。相反 大多數用戶界面列表控件, 其中一個列表由 相同類型或 同類型項目集合的項目中,TMS的先進聚 列表中的組件可以容納多晶型 項目。所有項目只需要從基類TCustomItem下降 和 可以添加任何繼承的項目。 TMS 先進聚組件列表來 有一大套預建列表 項目,但自定義項目類可以 由 TCustomItem基類的任何下降或已經提供的 類的加入。有 項目類顯示爲列表部分 項目,具有HTML格式的文本項目, 帶按鈕的文本項目,具有 的項目展開/摺疊行爲,項目與 圖像等等。項目可以是 添加在多形態列表中,或者在 設計時間,具有豐富的設計時間 編輯器和在運行時通過代碼。

+0

謝謝。太糟糕了,它已關閉源代碼。但如果我不能在自己的代碼中找到解決方案,那麼肯定會考慮購買。 – Andrew 2010-09-27 18:49:09

0

確保你的孩子的面板有名字。您可以覆蓋TCollection.Notify,如果操作是cnAdded,請確保該面板有一個名稱。