When I need to find the first node in TTreeView, I call TTreeNodes.GetFirstNode. However, sometimes I need to find the last node in the tree and there is no corresponding function TTreeNodes.GetLastNode.
I do not want to use Items[Count-1], as this leads to the fact that the whole tree passes through Result := Result.GetNext. Naturally, this only matters if there are many nodes in the tree views. I fully understand the virtues of virtual container controls, but so far I'm not going to switch to Virtual TreeView.
So far I have come up with the following:
function TTreeNodes.GetLastNode: TTreeNode;
var
Node: TTreeNode;
begin
Result := GetFirstNode;
if not Assigned(Result) then begin
exit;
end;
while True do begin
Node := Result.GetNextSibling;
if not Assigned(Node) then begin
Node := Result.GetFirstChild;
if not Assigned(Node) then begin
exit;
end;
end;
Result := Node;
end;
end;
Can anyone:
- Find a flaw in my logic?
- Suggest improvements?
Change 1
. , , AV , , , - , . , , - , , .