程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Delphi中動態加載TreeView信息

Delphi中動態加載TreeView信息

編輯:關於C語言

 

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.
 

本文出自 “迷茫後的短暫停留” 博客,請務必保留此出處http://suguiyang.blog.51cto.com/1035725/375681

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved