求编个对应的数据 和 设置方法:
以下代码,一句报错的也没有,就差设置数据库了,但我不会用,不懂咋搞,从未用过 这两控件。
[Delphi] 纯文本查看 复制代码 unit Unit5;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, vcl.fccombo, vcl.fcTreeCombo, Data.DB, Data.Win.ADODB,
Vcl.ComCtrls;
type
PNodeInfo = ^TNodeInfo;
TNodeInfo = record
ID: string;
FullName: string;
Url: string;
end;
TForm5 = class(TForm)
fcTreeCombo1: TfcTreeCombo;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure CreateChildTree(ParentNode: TTreeNode);
procedure CreateViewTree(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
procedure TForm5.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 TForm5.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 TForm5.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;
procedure TForm5.Button1Click(Sender: TObject);
begin
CreateViewTree(Sender);
end;
end.
|