递归获取treeview选中父节点的所有子节点

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure AddNodeList(NodeFirst:TTreeNode);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 NodeTest:TTreeNode;
begin
 NodeTest:= TreeView1.Selected.getFirstChild;  // 可以添加适当的节点过滤条件--genispan
 ListBox1.Items.Add(NodeTest.Text);
 AddNodeList(NodeTest);
end;

procedure TForm1.AddNodeList(NodeFirst:TTreeNode);  
begin
 if NodeFirst.getNextSibling<>nil then
   begin
   ListBox1.Items.Add(NodeFirst.getNextSibling.Text);
   AddNodeList(NodeFirst.getNextSibling);
   end;
end;

end.

你可能感兴趣的:(递归获取treeview选中父节点的所有子节点)