for I := 0 to TreeView1.Items.Count - 1 do TreeView1.Items[I];
递归. var j:integer; procedure TForm1.Button1Click(Sender: TObject); var i:byte; begin j:=0; for i:=0 to Treeview1.Items.Count-1 do begin EnumChildNode(TreeView1.Items[i]);end; Showmessage(InttoStr(j)); end;procedure TForm1.EnumChildNode(T: TTreeNode); var i:byte; begin if not Assigned(T) then Exit; if T.Count>0 then for i:=0 to T.Count-1 do begin inc(j); EnumChildNode(T[i]);end; end; 把INC(J)换成返回结点的函数就行了.
同意一楼, 二楼的代码看不懂,TTreeNode对象有count吗?
function TreeFindItem(Sender:TTreeView;Node:TTreeNode;FieldList_Name:String):TTreeNode;
begin if Node=nil then begin Node:=Sender.Items.GetFirstNode; end; if Node<>nil then begin Node:=Node.GetFirstChild; end; if (Node <>nil) and ( Node.Text<>FieldList_Name) then begin repeat Node:=Node.getNextSibling; until (Node=nil) or ( Node.Text=FieldList_Name) end; Result:=Node;end; function TreeAddItem(Sender:TTreeView;ItemList:TStrings;Book:TBook;Resort:Boolean):TTreeNode; var ThisNode,Node:TTreeNode; i:integer; begin Node:=nil; for i:=0 to ItemList.Count-1 do begin ThisNode:=TreeFindItem(Sender,Node,ItemList[i]); if ThisNode<>nil then Node:=ThisNode; if ThisNode=nil then begin if i<ItemList.Count-1 then begin if i=0 then Node:=Sender.Items.Add(Node,ItemList[i]); if i<>0 then Node:=Sender.Items.AddChild(Node,ItemList[i]); end; if i=ItemList.Count-1 then begin Node:=Sender.Items.AddChildObject(Node,ItemList[i],Book); end; end; Node.StateIndex:=Node.Level+1; end; Result:=Node; end; function TreeGetItem(Sender:TTreeView;ItemList:TStrings):TTreeNode; begin Result:=TreeAddItem(Sender,ItemList,nil,False); end;
TreeView1.Items[I];
var
j:integer;
procedure TForm1.Button1Click(Sender: TObject);
var
i:byte;
begin
j:=0;
for i:=0 to Treeview1.Items.Count-1 do begin
EnumChildNode(TreeView1.Items[i]);end;
Showmessage(InttoStr(j));
end;procedure TForm1.EnumChildNode(T: TTreeNode);
var
i:byte;
begin
if not Assigned(T) then Exit;
if T.Count>0 then
for i:=0 to T.Count-1 do begin
inc(j);
EnumChildNode(T[i]);end;
end;
把INC(J)换成返回结点的函数就行了.
二楼的代码看不懂,TTreeNode对象有count吗?
begin
if Node=nil then
begin
Node:=Sender.Items.GetFirstNode;
end;
if Node<>nil then
begin
Node:=Node.GetFirstChild;
end;
if (Node <>nil) and ( Node.Text<>FieldList_Name) then
begin
repeat
Node:=Node.getNextSibling;
until (Node=nil) or ( Node.Text=FieldList_Name)
end;
Result:=Node;end;
function TreeAddItem(Sender:TTreeView;ItemList:TStrings;Book:TBook;Resort:Boolean):TTreeNode;
var ThisNode,Node:TTreeNode;
i:integer;
begin
Node:=nil;
for i:=0 to ItemList.Count-1 do
begin
ThisNode:=TreeFindItem(Sender,Node,ItemList[i]);
if ThisNode<>nil then
Node:=ThisNode;
if ThisNode=nil then
begin
if i<ItemList.Count-1 then
begin
if i=0 then
Node:=Sender.Items.Add(Node,ItemList[i]);
if i<>0 then
Node:=Sender.Items.AddChild(Node,ItemList[i]);
end;
if i=ItemList.Count-1 then
begin
Node:=Sender.Items.AddChildObject(Node,ItemList[i],Book);
end;
end;
Node.StateIndex:=Node.Level+1;
end;
Result:=Node;
end;
function TreeGetItem(Sender:TTreeView;ItemList:TStrings):TTreeNode;
begin
Result:=TreeAddItem(Sender,ItemList,nil,False);
end;