以下程序遍历所有子节点,包括自己,稍微改一下,计数总没问题!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure GetAllSubNode(SelNode:TTreeNode);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.GetAllSubNode(SelNode: TTreeNode);
var
TN:TTreeNode;
begin
ListBox1.Items.Add(SelNode.Text);
if SelNode.HasChildren then
begin
TN:=SelNode.getFirstChild;
GetAllSubNode(TN);
TN:=TN.getNextSibling ;
while TN<>nil do
begin
GetAllSubNode(TN);
TN:=TN.getNextSibling ;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Clear ;
GetAllSubNode(TreeView1.selected);
end;
end.