Delphi中动态加载TreeView信息

2016-08-22 10:20:08来源:http://suguiyang.blog.51cto.com/1035725/375681作者:su_guiyang人点击




unit Unit3;


interface


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


type


PNodeInfo=^TNodeInfo;
TNodeInfo=record
ID:string;
FullName:string;
Url:string;
end;


TForm3 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
TreeView1: TTreeView;
Button1: TButton;
procedure CreateChildTree(ParentNode: TTreeNode);
procedure CreateViewTree(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form3: TForm3;


implementation


{$R *.dfm}


procedure TForm3.CreateChildTree(ParentNode: TTreeNode);
var
Query:TADOQuery;
ChildNode:TTreeNode; //孩子结点
ChildNodeInfo:PNodeInfo; //孩子结点信息
begin
Query:=TADOQuery.Create(nil);
with Query do
begin
Connection:=ADOConnection1;
SQL.Add('SELECT ID,FullName,Url FROM PInfo WHERE ParentID = '''+PNodeInfo(ParentNode.Data)^.ID+''''); //获取孩子结点信息
Open;
while not Eof do
begin
New(ChildNodeInfo);
ChildNodeInfo^.ID:=FieldByName('ID').AsString;
ChildNodeInfo^.FullName:=FieldByName('FullName').AsString;
ChildNodeInfo^.Url:=FieldByName('Url').AsString;
ChildNode:=TreeView1.Items.AddChildObject(ParentNode,(ChildNodeInfo^.FullName),ChildNodeInfo); //添加孩子结点,并关联孩子结点信息
CreateChildTree(ChildNode); //进行递归
Next;
end;
Close;
end;
end;


procedure TForm3.CreateViewTree(Sender: TObject);
var
BootNode:TTreeNode; //根结点
BootNodeInfo:PNodeInfo; //根结点信息
begin
with ADOQuery1 do
begin
SQL.Clear;
SQL.Add('SELECT ID,FullName,Url FROM PInfo WHERE ParentID IS NULL'); //获取根结点信息
Open;

New(BootNodeInfo);
BootNodeInfo^.ID:=FieldByName('ID').AsString;
BootNodeInfo^.FullName:=FieldByName('FullName').AsString;
BootNodeInfo^.Url:=FieldByName('Url').AsString;
TreeView1.Items.Clear;
BootNode:=TreeView1.Items.AddChildObject(nil,(BootNodeInfo^.FullName),BootNodeInfo); //添加根结点,并关联根结点信息
Close;
end;
CreateChildTree(BootNode); //创建子树
TreeView1.FullExpand; //展开所有树结点
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
CreateViewTree(Sender);
end;


procedure TForm3.TreeView1DblClick(Sender: TObject);
var
TNode:TTreeNode;
X,Y:Integer;
begin
//获取双击的结点
with TreeView1 do
begin
X:=ScreenToClient(Mouse.CursorPos).X;
Y:=ScreenToClient(Mouse.CursorPos).Y;
TNode:=GetNodeAt(X,Y);
end;
if (TNode<>nil) and (TNode=TreeView1.Selected) then
ShowMessage(PNodeInfo(TNode.Data)^.Url); //显示双击结点的信息
end;


end.


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台