Author: Tomas Rutkauskas
I have a 3 level TTreeview. The nodes in levels 2 and 3 must have unique captions
(text). Items will be added in a loop so I can't check the "Selected" text against
the data to be entered. However, I will know which node where data entry will
begin. If adding child nodes to a node on level 2 for example, I assume I need to
loop through the children of the particular parent node of the node on level 2 and
check the text property? Does this make sense?
Answer:
Yes. Use the edited nodes Parent.GetfirstChild to get a reference to the first
child node of that parent. Then use that nodes GetNextSibling to find the next node
on that level to examine, and so on. Untested:
1 function IsDuplicateNode(aNode: TTreenode): Boolean;
2 var3 walker: TTreenode;
4 begin5 Assert(Assigned(aNode), 'Need a node to examine!');
6 if Assigned(aNode.Parent) then7 walker := aNode.Parent.GetFirstChild
8 else9 walker := TTreeview(aNode.Treeview).Items[0];
10 Result := False;
11 while Assigned(walker) do12 begin13 if (walker <> aNode) and AnsiSametext(walker.Text, aNode.Text) then14 begin15 Result := true;
16 Break;
17 end;
18 walker := walker.GetNextSibling;
19 end;
20 end;