代码
unit uMain;
interface
uses
windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB, StdCtrls, Buttons, ExtCtrls, DBCtrls,
Grids, DBGrids, Menus;
type
TForm1 = class(TForm)
AdoConn: TADOConnection;
Query1: TADOQuery;
TreeView1: TTreeView;
btnTree: TButton;
Button2: TButton;
TreeView2: TTreeView;
Button3: TButton;
ListBox1: TListBox;
BitBtn1: TBitBtn;
btnInsert: TBitBtn;
btnView: TBitBtn;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
DataSource1: TDataSource;
Edit1: TEdit;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure btnTreeClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure btnInsertClick(Sender: TObject);
procedure btnViewClick(Sender: TObject);
procedure TreeView2Change(Sender: TObject; Node: TTreeNode);
private
{ Private declarations }
FCurNode:TTreeNode;
procedure CreateTree;
public
{ Public declarations }
end;
var
Form1: TForm1;
type
PIndex=^TIndex;
TIndex=Record
Name:String;
id:integer;
Pid:integer;
tag:integer;
end;
implementation
{$R *.dfm}
procedure TForm1.CreateTree;
var
CurValue:PIndex; //指向关联的指针
CurNode:TTreeNode; //当前节点
CurID:Integer; //节点的标识号
begin
CurNode:=nil;
CurID:=0;
with Query1 do
begin
TreeView2.Items.Clear;
TreeView2.Items.BeginUpdate;
First;
while not eof do
begin
New(CurValue);
with CurValue^ do
begin
Name:=FieldByName('NodeName').AsString;
id:=FieldByName('id').AsInteger;
Pid:=FieldByName('pid').AsInteger;
tag:=FieldByName('tag').AsInteger;
end;
while (CurID<>CurValue.Pid) do
//if CurNode<>nil then
begin
CurNode:=CurNode.Parent;
CurID:=PIndex(CurNode.Data).id;
end;
CurNode:=TreeView2.Items.AddChildObject(CurNode,CurValue^.Name,CurValue);
CurID:=PIndex(CurNode.Data).id;
Next;
end;
TreeView2.Items.EndUpdate;
TreeView2.FullExpand;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AdoConn.Open;
with Query1 do
begin
Close;
sql.Clear;
sql.Add('select * from k_tree');
sql.Add('order by id asc');
Open;
end;
TreeView1.FullExpand;
end;
procedure TForm1.btnTreeClick(Sender: TObject);
begin
CreateTree;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
TreeView1.SaveToFile('d: 22.txt');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
TreeView2.LoadFromFile('d: 22.txt');
TreeView2.FullExpand;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Node:TTreeNode;
I:integer;
begin
ListBox1.Items.Clear;
with TreeView1 do
begin
for i:=0 to Items.Count-1 do
begin
ListBox1.Items.Add(IntTostr(Items[i].Level)+' '+Items[i].Text+IntTostr(Items[i].AbsoluteIndex));
end;
end;
end;
procedure TForm1.btnInsertClick(Sender: TObject);
var
Node:TTreeNode;
I:integer;
begin
ListBox1.Items.Clear;
with Query1 do
begin
Close;
SQL.Clear;
sql.Add('delete from k_tree');
ExecSQL;
Close;
sql.Clear;
sql.Add('select * from k_tree');
Open;
end;
with TreeView1,Query1 do
begin
for i:=0 to Items.Count-1 do
begin
Query1.Append;
Query1.FieldByName('id').Value:=I;
if Items[i].IsFirstNode then
Query1.FieldByName('pid').Value:=0 else
Query1.FieldByName('pid').Value:=Items[i].Parent.AbsoluteIndex;
Query1.FieldByName('NodeName').Value:=Items[i].Text;
Query1.FieldByName('tag').Value:=Items[i].AbsoluteIndex;
Query1.FieldByName('pLevel').Value:=Items[i].Level;
Query1.Post;
//ListBox1.Items.Add(IntTostr(Items[i].Level)+' '+Items[i].Text+IntTostr(Items[i].AbsoluteIndex));
end;
end;
end;
procedure TForm1.btnViewClick(Sender: TObject);
var
pid,pLevel:integer;
FNode:TTreeNode;
CurValue:PIndex;
CurNode:TTreeNode;
Curid:integer;
begin
with Query1,TreeView2 do
begin
Items.Clear;
First;
CurNode:=nil;
Curid:=-1;
while not eof do
begin
New(CurValue);
CurValue^.id:=FieldByName('id').AsInteger;
CurValue^.Name:=FieldByName('NodeName').AsString;
CurValue^.Pid:=FieldByName('pid').AsInteger;
if Curid<>CurValue^.id then
//-----
pid:=FieldByName('pid').AsInteger;
pLevel:=FieldByName('pLevel').AsInteger;
if (pid=0) and (pLevel=0) then
FNode:=Items.AddChild(nil,FieldByName('NodeName').asstring);
Next;
end;
end;
end;
procedure TForm1.TreeView2Change(Sender: TObject; Node: TTreeNode);
begin
Edit1.Text:=IntTostr(PIndex(Node.Data).tag);
FCurNode:=Node;
end;
end.