{************************************************* ******}
{}
{} operating the TreeView
{}
{Copyright (C) 2008 South Wing studio}
{}
{******************** ***********************************}
// ================================================ ==============================
// this unit implements the data associated with TreeView, each method parameter as follows:
//
// AId: to increment field for serial number to identify the node.
// AParentId: used to represent the parent of the current node.
// ACaption: used to represent the contents in the tree to be displayed.
// ================================================ ==============================
unit dbtree;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, ComCtrls, DB, ADODB;
{------------------------------------------------- ------------------------------
procedure name: MakeTree
Description: create a tree
author: cxg
date: 2008.08.21
parameters: Query: TADOQuery; TableName: string; TreeView: TTreeView; AParentId, AId: string
return value: None
------------------------------- } ------------------------------------------------
Procedure MakeTree (Query: TADOQuery; TableName: string; TreeView: TTreeView; AParentId, AId: string);
{------------------------------------------------- ------------------------------
procedure name: AddNode
Description: increased tree node
of: cxg
date: 2004.12.07
parameters: Query: TADOQuery; TreeView: TTreeView
parameters: Query: an associated database; the TreeView: tree used
return value: None
----------------------- -------------------------------------------------- } ------
Procedure the AddNode (Query: TADOQuery; the TreeView: TTreeView; AParentId, ACaption: String);
{------------------------------------------------- ------------------------------
procedure name: AddChildNode
Description: increase the child node tree
author: cxg
date: 2004.12.07
parameters : Query: TADOQuery; TreeView: TTreeView
parameters: Query: an associated database; the TreeView: tree used
return value: None
---------------------- -------------------------------------------------- } -------
Procedure AddChildNode (Query: TADOQuery; the TreeView: TTreeView; AParentId, AId, ACaption: String);
{--------------------- -------------------------------------------------- --------
procedure name: AddTreeNode
Description: add nodes (internal process)
author: cxg
date: 2004.12.07
parameters: Query: TADOQuery; TreeView: TTreeView ; bj: boolean = false
Parameters: Query: an associated database; the TreeView: used tree; BJ: whether to move the focus to the currently selected node
Return Value: None
------------------ -------------------------------------------------- } -----------
Procedure AddTreeNode (Query: TADOQuery; the TreeView: TTreeView; BJ: = Boolean to false);
{------------------------------------------------- ------------------------------
procedure name: DelTree
Description: delete nodes (including the deletion of all child nodes under this node)
author : CXG
date: 2004.12.07
parameters: query: TAdoQuery; TreeView: TTreeView
parameters: Query: an associated database; the TreeView: tree used
return value: None
-------------- -------------------------------------------------- } ---------------
Procedure deltree (Query: TADOQuery; the TreeView: TTreeView; AID of the: String);
{------------------------------------------------- ------------------------------
procedure name: Treechange
Description: selecting a node in the tree, the cursor is moved to the database the node corresponding to the selected recording
oF: CXG
date: 2004.12.07
parameters: query: TADOQuery; node: TTreenode
parameters: Query: an associated database; the TreeView: tree used
return value: None
------ -------------------------------------------------- } -----------------------
Procedure Treechange (Query: TADOQuery; Node: TTreenode);
{------------------------------------------------- ------------------------------
procedure name: Treeedit
Description: the contents of the node in the tree has been modified, the modified updates back to the database
author: cxg
date: 2004.12.07
parameters: query: TADOQuery; text: string
parameters: Query: database associated; TreeView: used tree
return value: None
--------- -------------------------------------------------- } --------------------
Procedure TreeEdit (Query: TADOQuery; text: String; ACaption: String);
{------------------------------------------------- ------------------------------
procedure name: treeselect
Description: database cursor has moved, the focus moves to the corresponding tree node
oF: CXG
date: 2004.12.07
parameters: query: Tadoquery; TreeView: TTreeView
parameters: Query: an associated database; the TreeView: tree used
return value: None
----------- -------------------------------------------------- } ------------------
Procedure treeselect (Query: TADOQuery; the TreeView: TTreeView; AId: String);
{------------------------------------------------- ------------------------------
function name: TreeFind
Description: find the given content, and move the cursor in the database, At the same time move the focus to the corresponding node in the tree
author: cxg
date: 2004.12.07
parameters: TreeView: TTreeView; text: string
parameter Description: TreeView: tree text used: what to look for
return values: True: found False: Can not find
----------------------------------------------- } --------------------------------
function TreeFind (the TreeView: TTreeView; text: String): Boolean;
implementation
var
List: a TStringList; // record for each node in the tree and its id corresponding to achieve quickly find
procedure MakeTree(Query: TADOQuery; TableName: string; TreeView: TTreeView;AParentId,AId:string);
begin
TreeView.Items.BeginUpdate;
list.Clear;
TreeView.items.clear;
if query.Active then query.Close;
Query.SQL.Text := 'SELECT * FROM ' + TableName + ' ORDER BY '+AParentId+','+AId;
Query.Open;
query.DisableControls;
TreeView.Items.Clear;
list.Clear;
List.Sorted := True;
query.First;
while not Query.Eof do
begin
addtreenode(Query, TreeView); //依次增加所有的节点
Query.Next;
end;
TreeView.Items.EndUpdate;
query.EnableControls;
if treeview.Items.Count < 1 then exit;
treeview.Select(treeview.Items.Item[0]);
treeview.SetFocus;
end;
addtreenode Procedure (Query: TADOQuery; the TreeView: TTreeView; BJ: = Boolean to false);
var
index: Integer;
the Node: TTreeNode;
the begin
. Query.FieldByName IF ( 'the PID') = 0 the then AsInteger ParentID = {0, top node}
the node: TreeView.Items.AddChildObject = (. nil, Query.FieldByName ( 'CAPTION') AsString, query.GetBookmark)
// add nodes, and the node corresponding to the additional data recorded into the tag data provided by the node
the else
the begin
Index: = List.IndexOf (Query.FieldByName ( 'the PID') AsString.);
the Node: = TreeView.Items.AddChildObjectFirst (TTreeNode (List.Objects [Index]),
Query.FieldByName ( 'CAPTION') AsString. , query.GetBookmark);
// child node increases, and the node corresponding to the additional data recorded into the tag data provided by the node
End;
// add information of the current node to the list, to achieve the list quickly Find function nodes.
List.AddObject(Query.FieldByName('ID').AsString, Node);
if bj then
begin
treeview.Select(node);
treeview.SetFocus;
end;
end;
deltree Procedure (Query: TADOQuery; the TreeView: TTreeView; AID of the: String);
var
Node: TTreenode;
// remove all the nodes in the node currently selected (currently selected node is not deleted)
Procedure delnode (Node: TTreenode);
var
I : Integer;
childNode: TTreenode;
the begin
for I: = 0 to node.Count - do. 1
the begin
childNode: = node.Item [I]; // get all the nodes under the current node
query.GotoBookmark (childnode.Data); / / record corresponding to node
list.Delete (list.IndexOf (query.FieldByName (AID) .AsString)); // delete data list
query.Delete; // delete the corresponding record
if node.HasChildren then delnode (childnode); // have child nodes are recursively until all the data deletion is complete
end;
end;
the begin
the Node: = treeview.Selected;
IF the then Exit the Node = nil;
// remove all nodes (not delete the currently selected node) node under the currently selected
delnode (the Node);
// delete the node currently selected
query.GotoBookmark ( node.Data);
list.Delete (list.IndexOf (query.FieldByName (AID of the) .AsString));
query.Delete;
node.Delete;
TreeView.SetFocus;
End;
AddChildNode Procedure (Query: TADOQuery; the TreeView: TTreeView; AParentId, AId, ACaption: String);
var
ID: Integer;
the begin
ID: = query.FieldByName (AId) .AsInteger; // note of the current node number
query.Append;
query.FieldByName (AParentId) .AsInteger: = id ; // number of the new parent node of the child node is the increase ID
query.FieldByName (ACaption) .AsString: = 'new child node';
query.post;
addtreenode (Query, TreeView, to true);
End;
the AddNode Procedure (Query: TADOQuery; the TreeView: TTreeView; AParentId, ACaption: String);
var
PID: Integer;
the begin
PID: = query.FieldByName (AParentId) .AsInteger; // note of the parent node of the current node number
query.Append;
query.FieldByName (AParentId) .AsInteger: = pid ; // number of the new parent node of the child node is the increase PID
query.FieldByName (ACaption) .AsString: = 'new node';
query.post;
addtreenode (Query, TreeView , to true);
End;
procedure Treechange(query: TADOQuery; node: TTreenode);
begin
query.GotoBookmark(node.Data);
end;
procedure TreeEdit(query: TADOQuery; text: string;ACaption:string);
begin
query.Edit;
query.FieldByName(ACaption).AsString := text;
query.post;
end;
procedure treeselect(query: Tadoquery; TreeView: TTreeView;AId:string);
var
index: integer;
Node: TTreeNode;
begin
Index := List.IndexOf(Query.FieldByName(AId).AsString);
Node := TTreeNode(List.Objects[Index]);
treeview.Selected := Node;
treeview.SetFocus;
end;
function TreeFind(TreeView: TTreeView; text: string): boolean;
var
i: integer;
begin
Result := false;
for i := 0 to treeview.Items.Count - 1 do
begin
if treeview.Items.Item[i].Text = text then
begin
treeview.Select(treeview.Items.Item[i]);
treeview.SetFocus;
Result := true;
exit;
end;
end;
end;
initialization
List := TStringList.Create;
finalization
list.Free;
end.
// Call presentation (drag and drop mobile node) --------------------------------------- ------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
DB, ADODB, StdCtrls, ExtCtrls, Grids, DBGrids, ComCtrls, dialogs;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
DBGrid1: TDBGrid;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Splitter1: TSplitter;
Splitter2: TSplitter;
btn2: TButton;
Button4: TButton;
Edit1: TEdit;
ADOQuery: TADOQuery;
ds1: TDataSource;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure Button3Click(Sender: TObject);
procedure TreeView1Edited(Sender: TObject; Node: TTreeNode; var S: string);
procedure btn2Click(Sender: TObject);
procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button4Click(Sender: TObject);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
was
Form1: TForm1;
implementation
uses dbtree;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MakeTree(ADOQuery, 'dir', TreeView1,'pid','id'); //创建树
end;
TForm1.Button3Click Procedure (Sender: TObject);
the begin
addNode (ADOQuery, TreeView1, 'PID', 'Caption'); // add nodes to
end;
TForm1.Button2Click Procedure (Sender: TObject);
the begin
addchildnode (ADOQuery, TreeView1, 'PID', 'ID', 'Caption'); // child node increases
end;
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
treechange(adoquery, node);
end;
procedure TForm1.TreeView1Edited(Sender: TObject; Node: TTreeNode;
var S: string);
begin
Treeedit(adoquery, s, 'caption');
end;
TForm1.btn2Click Procedure (Sender: TObject);
the begin
deltree (ADOQuery, TreeView1, 'the above mentioned id'); // delete tree
end;
procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
treeselect(adoquery, treeview1,'id');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if not TreeFind(treeview1,edit1.text) then
showmessage('找不到。');
end;
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := False ;
if (source is TTreeView) or (source is TListview) then
Accept := true ;
end;
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
tSrcNode ,tDesNode: TTreeNode ;
tSrcID ,tDesID : Integer ;
sSql : String ;
lSrcItem : TListItem ;
DestNodeId:string;
begin
tSrcID := 0 ;tDesID := 0 ;
tDesNode := TTreeview(Sender).GetNodeAt(X,Y);
Treechange(ADOQuery,tDesNode);
DestNodeId:=ADOQuery.FieldValues['id'];
if Source is TTreeview then
begin
TTreeview(Sender).Items.BeginUpdate;
tSrcNode := TTreeview(Source).Selected ;
tSrcNode.Moveto(tDesNode,naAddChild);
Treechange(ADOQuery,tSrcNode);
ADOQuery.Edit;
ADOQuery.FieldValues['pid']:=DestNodeId;
ADOQuery.Post;
TTreeView (transmitter) .Items.EndUpdate;
end
end;
end.