Author: Tomas Rutkauskas
I would like to populate a TTreeView from a simple file with the following structure
Key: Integer (unique)
Name: String (description)
Parent: Integer (key of parent in treeview)
I assume that the key and parent fields are all I need to build the treeview
(parent = 0 would be a root node)
Answer:
I would break this down into two steps:
1) Read the file into memory
2) Populate the treeview using a recursive function
1) One method of doing this would be by building a TCollection/ TCollectionItem
pair of classes. The TCollectionItems just need three fields:
1 TInputItem = class(TCollectionItem)
2 private
3 fKey: integer;
4 fName: string;
5 fParent: integer;
6 public
7 property Key: integer read fKey write fKey;
8 property Name: string read fName write fName;
9 property Parent: integer read fParent write fParent;
10 end;
Note: using properties is not strictly necessary, but is good style as it allows
easier subsequent amendment.
Now we could use a standard TCollection to hold our TInputItems but it is neater to
have a descendent of this too:
11 TInputCollection = class(TCollection)
12 public
13 function AddItem(const AName: string; AKey, AParent: integer): TInputItem;
14 property InputItem[index: integer]: TInputItem read GetInputItem; default;
15 end;
Creating a default property like InputItem above makes coding very tidy. It allows
us to do the following:
16 var
17 InputCollection: InputCollection;
18 ix: integer;
19
20 InputCollection := TInputCollection.Create(TInputItem);
21 InputCollection.AddItem('First', 1, 0);
22 InputCollection.AddItem('Second', 2, 0);
23 InputCollection.AddItem('FirstChild', 3, 1);
24
25 for ix := 0 to InputCollection.Count - 1 do
26 if InputCollection[ix].Parent = 0 then
27 {DoSomething};
The last line, because of the index property being declared default, is the same as:
28 if InputCollection.InputItem[ix].Parent = 0 then
29 {DoSomething;}
30
31 //Without the property at all, you would code:
32
33 if TInputItem(InputCollection.Items[ix]).Parent = 0 thenDoSomething;
34 {DoSomething;}
In order to support the above, the implementation of the two methods:
35
36 function TInputCollection.AddItem(const AName: string; AKey, AParent: integer):
37 TInputItem;
38 begin
39 Result := Add as TInputItem;
40 Result.Key := AKey;
41 Result.Name := AName;
42 Result.Parent := AParent;
43 end;
44
45 function TInputCollection.GetInputItem(index: integer): TInputItem;
46 begin
47 Result := Items[ix] as TInputItem;
48 end;
49
50 //We can now design an overall structure of a PopulateTree procedure:
51
52 procedure PopulateTree(tv: TTreeView);
53 var
54 ic: TInputCollection;
55 begin
56 ic := TInputCollection.Create(TInputItem);
57 try
58 LoadTreeItems(ic);
59 PopulateTreeItems(tv, nil, ic, 0);
60 finally
61 ic.Free;
62 end;
63 end;
64
65 //LoadTreeItems can be tested via code similar to:
66
67 procedure LoadTreeItems(ic: TInputCollection);
68 begin
69 ic.AddItem('First', 1, 0);
70 ic.AddItem('Second', 2, 0);
71 ic.AddItem('FirstChild', 3, 1);
72 end;
before replacing with your own loop through your input file. PopulateTreeItems is
passed the treeview, the parent node and the parent id and it is a recursive
routine.
2) Having done all the above, this part is now very easy. PopulateTreeItems
iterates through the collection looking for items that match the passed parent id.
For each item that matches, it adds a treenode and then calls PopulateTreeItems
passing itself as the parent:
73
74 procedure PopulateTreeItems(tv: TTreeView; pnode: TTreeNode; ic: TInputCollection;
75 parent: integer);
76 var
77 node: TTreeNode;
78 ix: integer;
79 begin
80 for ix := 0 to ic.Count - 1 do
81 begin
82 if ic[ix].Parent = parent then
83 begin
84 node := tv.Items.Add(pnode, ic[ix].Name);
85 PopulateTreeItems(tv, node, ic, ic[ix].Key); {recursive call}
86 end;
87 end;
88 end;
I apologise in advance if there are problems with the above code. It is completely untested. In practice, I don't do things quite like that, but populate treenodes on demand via the OnExpand event handler.
|