Mirror

Ensure that every node in a TTreeView is unique (2) (Views: 707)


Problem/Question/Abstract:

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:

function IsDuplicateNode(aNode: TTreenode): Boolean;
var
  walker: TTreenode;
begin
  Assert(Assigned(aNode), 'Need a node to examine!');
  if Assigned(aNode.Parent) then
    walker := aNode.Parent.GetFirstChild
  else
    walker := TTreeview(aNode.Treeview).Items[0];
  Result := False;
  while Assigned(walker) do
  begin
    if (walker <> aNode) and AnsiSametext(walker.Text, aNode.Text) then
    begin
      Result := true;
      Break;
    end;
    walker := walker.GetNextSibling;
  end;
end;

<< Back to main page