Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to make a TCollectionItem contain a TCollection Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
55
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I would like to create a component that contains a TCollection with 
TCollectionItems. Then I want to contain another TCollection with TCollectionItems 
within the TCollectionItems. I am trying to create a collection of sections, which 
would contain a collection of items for each section.

Answer:

It's not difficult to implement such functionality. One thing you need to care 
about is the valid Owner for your collections, presumably, the main control would 
be the best choice. Below is an example of such a component:


1   { ... }
2   type
3     TYourCollectionItem = class;
4     TYourCollection = class;
5     TColControl = class;
6   
7     TYourCollectionItem = class(TCollectionItem)
8     protected
9       FFirstString: string;
10      FChildCollection: TYourCollection;
11      procedure SetIndex(Value: Integer); override;
12      function GetDisplayName: string; override;
13    public
14      constructor Create(Collection: TCollection); override;
15      destructor Destroy; override;
16      procedure Assign(Source: TPersistent); override;
17    published
18      property FirstString: string read FFirstString write FFirstString;
19      property ChildCollection: TYourCollection read FChildCollection write
20        FChildCollection;
21    end;
22  
23    TYourCollection = class(TOwnedCollection)
24    protected
25      function GetItem(Index: Integer): TYourCollectionItem;
26      procedure SetItem(Index: Integer; Value: TYourCollectionItem);
27    public
28      constructor Create(AOwner: TPersistent);
29      property Items[Index: Integer]: TYourCollectionItem read GetItem write SetItem;
30    end;
31  
32    TColControl = class(TComponent)
33    protected
34      FCollection: TYourCollection;
35    public
36      constructor Create(AOwner: TComponent); override;
37      destructor Destroy; override;
38    published
39      property Collection: TYourCollection read FCollection write FCollection;
40    end;
41  
42    { ... }
43  
44  constructor TYourCollectionItem.Create(Collection: TCollection);
45  begin
46    inherited Create(Collection);
47    FChildCollection := TYourCollection.Create(Collection.Owner);
48  end;
49  
50  destructor TYourCollectionItem.Destroy;
51  begin
52    FChildCollection.Free;
53    inherited Destroy;
54  end;
55  
56  procedure TYourCollectionItem.SetIndex(Value: Integer);
57  begin
58    inherited SetIndex(Value);
59    ShowMessage(IntToStr(Value));
60  end;
61  
62  function TYourCollectionItem.GetDisplayName: string;
63  begin
64    Result := FFirstString;
65  end;
66  
67  procedure TYourCollectionItem.Assign(Source: TPersistent);
68  begin
69    FFirstString := TYourCollectionItem(Source).FFirstString;
70    FChildCollection.Assign(TYourCollectionItem(Source).ChildCollection);
71  end;
72  
73  constructor TYourCollection.Create(AOwner: TPersistent);
74  begin
75    inherited Create(AOwner, TYourCollectionItem);
76  end;
77  
78  function TYourCollection.GetItem(Index: Integer): TYourCollectionItem;
79  begin
80    Result := TYourCollectionItem(inherited GetItem(Index));
81  end;
82  
83  procedure TYourCollection.SetItem(Index: Integer; Value: TYourCollectionItem);
84  begin
85    inherited SetItem(Index, Value);
86  end;
87  
88  constructor TColControl.Create(AOwner: TComponent);
89  begin
90    inherited Create(AOwner);
91    FCollection := TYourCollection.Create(Self);
92  end;
93  
94  destructor TColControl.Destroy;
95  begin
96    FCollection.Free;
97    FCollection := nil;
98    inherited Destroy;
99  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC