delphi中父母表示法递归构造treeview(在递归函数之外查询数据库)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ComCtrls, StdCtrls;
type
  PTNodeInfo=^TNodeInfo;
  TNodeInfo=record
  id,preId:string;
  end;
type
  TArr=array of TNodeInfo;
type
  TForm1 = class(TForm)
    Button1: TButton;
    tree: TTreeView;
    Query1: TADOQuery;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure CreateTree(var arr:TArr);
    procedure InitArray(var arr:TArr);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.InitArray(var arr:TArr);
var

  Query:TADOQuery;
  i:integer;
  sql:string;
begin
  Query:=TADOQuery.Create(nil);
  Query.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/实验/递归父母表示/新建 Microsoft Office Access 应用程序.mdb;Persist Security Info=False';
  sql:= 'select * from table1';
  Query.Close;
  Query.SQL.Clear;
  Query.SQL.Add(sql);
  Query.Open;
  Query.First;
  SetLength(arr,Query.RecordCount);
  i:=0;
  while not Query.Eof do
  begin
    arr[i].id:=Query.fields[0].AsString;
    arr[i].preId:=Query.Fields[1].AsString;
    Inc(i);
    Query.Next;
  end;

end;

procedure TForm1.CreateTree(var arr:TArr);
var
  p:pTNodeInfo;
  procedure CreateTree(Pre:PTNodeInfo;preNode:TTreeNode;var arr:TArr);
  var
    pInfo:pTNodeInfo;
    node:TTreeNode;
    i:integer;
  begin
    i:=length(arr)-1;
    while i>=0 do
    begin
      new(pInfo);
      pInfo:=@arr[i];
      if pInfo.preId=pre.id then
      begin
        node:=tree.Items.AddChild(preNode,pInfo.id);
        node.Data:=pInfo;
        CreateTree(pInfo,node,arr);
      end;
    i:=i-1;
    end;
  end;
begin
  new(p);
  p.id:='000';
  createTree(p,nil,arr);
  tree.FullExpand;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
arr:TArr;
i:Integer;
begin
  InitArray(arr);
  CreateTree(arr);
  memo1.Clear;
  for i:=0 to tree.Items.Count-1 do
  memo1.Lines.Add(ptnodeinfo(tree.Items[i].data).Id+'  '+ptnodeinfo(tree.Items[i].data).preId);

end;

end.                               

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

delphi中父母表示法递归构造treeview(在递归函数之外查询数据库)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭