Copy Code code as follows:
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Comctrls, DB, ADODB;
Type
Pnodeinfoex = ^tnodeinfoex;
Tnodeinfoex = Packed Record
Nodeid:integer;
Parentid:integer;
Nodetype:integer;
chnnodetitle:string;
Imageindex:smallint;
Selectedindex:smallint;
End
TForm1 = Class (Tform)
Tv1:ttreeview;
Btn1:tbutton;
Qry1:tadoquery;
Procedure Btn1click (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Private
{Private declarations}
function Staticbuildtree (treeview:ttreeview): Boolean;
function Addtreeitem (Treeview:ttreeview; ADDNODEINFO:PNODEINFOEX): Ttreenode;
function Findtreeitem (Treeview:ttreeview; Curnodeid:integer): Ttreenode;
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
function Tform1.staticbuildtree (treeview:ttreeview): Boolean;
Var
Addnodeinfo:pnodeinfoex;
Begin
Result: = False;
Qry1. LoadFromFile (' C:/adminixtree.xml '); Here is the XML file as the data source
treeview.items.beginupdate;//Remember to use beginupdate to temporarily turn off certain events that are triggered by adding data (such as onchange events) when you are adding data in bulk.
treeview.items.clear;//Empty the TreeView.
Try
Try
If Qry1. RecordCount >0 Then
Begin
Qry1. A;
While not qry1. Eof do
Begin
New (addnodeinfo);//Build Structure body
addnodeinfo^. NodeID: = Qry1. Fieldbyname (' node_id '). Asinteger;
addnodeinfo^. ParentID: = Qry1. Fieldbyname (' parent_id '). Asinteger;
addnodeinfo^. NodeType: = Qry1. Fieldbyname (' NodeType '). Asinteger;
addnodeinfo^. Chnnodetitle: = Qry1. Fieldbyname (' Chnnodetitle '). asstring;
addnodeinfo^. ImageIndex: = Qry1. Fieldbyname (' ImageIndex '). Asinteger;
addnodeinfo^. SelectedIndex: = Qry1. Fieldbyname (' SelectedIndex '). Asinteger;
Addtreeitem (Treeview,addnodeinfo)//To save the pointer of the structure to the Treeview
Qry1. Next;
End
End
Except
Application.messagebox (' Spanning tree node failure ', MB_ICONSTOP+MB_OK);
raise;//throws an exception to the superior
End
Qry1. Close;
Result: = True;
Finally
Treeview.Items.EndUpdate;
End
End
When adding a node, we should first judge whether the parent node or the child node is joined, and the basis of judgment is the parentid of the node in the existing tree node.
function Tform1.addtreeitem (Treeview:ttreeview; ADDNODEINFO:PNODEINFOEX): Ttreenode;
Var
Parentnode:ttreenode;
Begin
ParentNode: = Findtreeitem (treeview,addnodeinfo^. ParentID);
If parentnode <> Nil Then
Result: = Treeview.Items.AddChildObject (ParentNode, Trim (addnodeinfo.chnnodetitle), pointer (addnodeinfo))
Else
Result: = Treeview.Items.AddObject (ParentNode, Trim (addnodeinfo.chnnodetitle), pointer (addnodeinfo));
If Result<>nil Then
Begin
Result.imageindex: = Addnodeinfo.imageindex;
Result.selectedindex: = Addnodeinfo.selectedindex;
End
End
Here is to determine if there is a parent node.
function Tform1.findtreeitem (Treeview:ttreeview; Curnodeid:integer): Ttreenode;
Var
I:integer;
Begin
Result: = nil;
For I: = 0 to Treeview.items.count-1 do
Begin
If Curnodeid=pnodeinfoex (Treeview.items[i]. Data) ^. NodeID Then
Begin
Result: = Treeview.items[i];
Exit;
End
End
End
Spanning tree structure
Procedure Tform1.btn1click (Sender:tobject);
Begin
Staticbuildtree (TV1)
End
In the form of release must be the tree node in the structure of the pointer to release, for the dispose of why the mandatory transformation after the release, there have been special explanations, here is not tired
Procedure Tform1.formdestroy (Sender:tobject);
Var
I:integer;
Begin
For I: = 0 to TV1. Items.count-1 do
Begin
Dispose (Pnodeinfoex (TV1). Items[i]. Data))
End
End
End.
Copy Code code as follows:
How do I access a tree node?
Procedure Tform1.tv1mousedown (Sender:tobject; Button:tmousebutton;
Shift:tshiftstate; X, Y:integer);
Var
Pnode:ttreenode;
Begin
PNODE:=TV1. GetNodeAt (X,y);
if (Pnode<>nil) and (Button=mbleft) then
ShowMessage (Pnodeinfoex (pnode.data) ^. Chnnodetitle);
End