Delphi中动态加载TreeView信息

Posted jijm123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi中动态加载TreeView信息相关的知识,希望对你有一定的参考价值。

技术图片

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.
 

 

以上是关于Delphi中动态加载TreeView信息的主要内容,如果未能解决你的问题,请参考以下文章

DELPHI TreeView,动态添加父节点,子节点

wpf中怎么为treeview动态添加子菜单

delphi 请问在treeview控件中怎样判断单击一个项目实现某些功能?

c# 动态加载treeview控件,不是节点

在 adminlte-3 bootstrap-4 treeview 打开/隐藏页面加载后加载动态 AJAX 侧边栏菜单 jquery 不起作用

wpf treeview中动态创建的treeViewItem如何用右键触发菜单?