Delphi dynamic loading TreeView information

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; //Child node information 
the begin 
  Query: . = TADOQuery the Create ( nil );
   with Query do 
  the begin 
    Connection: = ADOConnection1; 
    SQL.Add ( ' the SELECT ID, the FullName, the Url the FROM PInfo the WHERE ParentID =' ' ' + PNodeInfo (ParentNode.Data) .ID + ^ ' ' ' ' ); // Get the child node information of 
    the Open;
     the while  Not Eof do 
    the begin 
      New (ChildNodeInfo); 
      ChildNodeInfo .ID ^: = FieldByName ( ' ID ' ) .AsString;
      ^ .FullName ChildNodeInfo:FieldByName = ( ' the FullName ' ) .AsString; 
      ChildNodeInfo ^ .url: = FieldByName ( ' the Url ' ) .AsString; 
      ChildNode: = TreeView1.Items.AddChildObject (ParentNode, (ChildNodeInfo ^ .FullName), ChildNodeInfo); // add child node and the child node information associated 
      CreateChildTree (ChildNode); // recursive 
      the Next;
     End ; 
    the Close; 
  End ;
 End ; 

Procedure TForm3.CreateViewTree (Sender: TObject);
 var 
  BootNode: TTreeNode; // root 
  BootNodeInfo: PNodeInfo; // root node information
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); // add the root node, the root node and link information 
    the Close;
   End ; 
  CreateChildTree (BootNode); // create a sub-tree 
  TreeView1.FullExpand; // expand all tree nodes 
End ;
 Procedure TForm3.Button1Click (Sender: TObject);
 the begin 
   CreateViewTree (Sender); 
End ; 

Procedure TForm3.TreeView1DblClick (Sender: TObject);
 var 
  TNode: TTreeNode; 
  the X- , the Y: Integer; 
the begin 
  // Get Double-junction 
  with of TreeView1do 
  the begin 
    X-: = while ScreenToClient (Mouse.CursorPos) .X;
    The Y: = while ScreenToClient (Mouse.CursorPos) .Y; 
    TNode: = The GetNodeAt (X-, the Y);
   End ;
   IF (TNode <> nil ) and (TNode = TreeView1.Selected) the then 
    the ShowMessage (PNodeInfo (TNode.Data) ^. URL); // display node information Double- 
End ; 

End .
 

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11408413.html