1、Delphi 中 dxTreelist 控件的使用 最近公司项目中用到 dxTreelist 控件,以前没怎么用过这个控件,该控件比 TreeView 功能更强大,要完全掌握还真不容易,下面是我个人做的一个 Demo,对该控件进行增删改遍历操作,以便自己以后忘了使用方法随时可以查吧。1.首先新建一个数据库。打开 SQL Server2005,新建数据库 TreeTest,新建表 DeviceTab,表的字段设计如下:2.新建 Delphi 应用程序选择 File-New-Application 新建应用程序,File-New-Data Module,新建一个数据库模块,添加 TADOConne
2、ction 和 TImageList 控件, 设置 ADOConnection的 LoginPrompt 为 false,给 ImageList 控件添加 7 个图片,该数据模块的Create 事件代码如下 :procedure Tdm.DataModuleCreate(Sender: TObject);beginif not con1.Connected thenbegin/设置连接字符串con1.ConnectionString:=Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;User ID=sa;Initia
3、l Catalog=TreeList;Data Source=ZHSQLEXPRESS;con1.Open; /打开数据库连接end;end;在窗体上放置 dxTreeList 控件和四个 Button 控件以及一个 Timer 控件设置Timer 控件的 Interval 属性为 100,各按钮相关事件及源码如下:unit Main;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, dxExEdtr, dxCntner, dxTL, dxTLClms,
4、ADODB, Menus,cxLookAndFeelPainters, StdCtrls, cxButtons, ExtCtrls;typeTfrmTree = class(TForm)dxTreeList1: TdxTreeList;btnAddNode: TcxButton;btnDelNode: TcxButton;btnUpdate: TcxButton;btnbl: TcxButton;tmr1: TTimer;procedure btnblClick(Sender: TObject);procedure btnAddNodeClick(Sender: TObject);proced
5、ure btnDelNodeClick(Sender: TObject);procedure btnUpdateClick(Sender: TObject);procedure tmr1Timer(Sender: TObject);procedure FormDestroy(Sender: TObject);private Private declarations public Public declarations procedure CreateColumn(tree:TdxTreeList;list:TStrings);procedure CreateImageColumn(tree:T
6、dxTreeList;Image:TImageList);procedure FillTree(node:TdxTreeListNode;pid:Integer;con:TADOConnection);function GetMaxId(con:TADOConnection):Integer;procedure DelTree(pid:Integer;con:TADOConnection);procedure UpdateTree(newname:string;id:Integer;con:TADOConnection);function GetCount(tree:TdxTreeList):
7、Integer;end;varfrmTree: TfrmTree;implementation$R *.dfmuses unitDM;varlist:TStrings;procedure TfrmTree.CreateColumn(tree:TdxTreeList;list:TStrings); /动态创建 dxTreeList 的列vari:Integer;col:TdxTreeListColumn;beginfor i:=0 to list.Count-1 dobegincol:=tree.CreateColumn(TdxTreeListMaskColumn);col.Caption:=l
8、isti; /显示标题col.ColIndex:=i; /列索引col.Name:=listi; /列标识col.Width:=90+10*i; /列宽end;end;procedure TfrmTree.CreateImageColumn(tree:TdxTreeList;Image:TImageList);/创建带图片的列varcol:TdxTreeListColumn;i:Integer;begincol:=tree.CreateColumn(TdxTreeListImageColumn);col.Width:=50;col.Name:=img;TdxTreeListImageColum
9、n(col).Images:=Image;for i:=0 to image.Count-1 do /关联图片beginTdxTreeListImageColumn(col).ImageIndexes.Add(IntToStr(i);TdxTreeListImageColumn(col).Values.Add(IntToStr(i);end;end;procedure TfrmTree.FillTree(node: TdxTreeListNode; pid: Integer;con:TADOConnection);/构造树vartmpnode:TdxTreeListNode;ADOQuery:
10、TADOQuery;beginADOQuery:=TADOQuery.Create(nil);tryADOQuery.Connection:=con;ADOQuery.Close;ADOQuery.SQL.Clear;ADOQuery.SQL.Text:=select * from DeviceTab where pid=+Inttostr(pid);ADOQuery.Open;with ADOQuery dobeginFirst;while not Eof dobeginif node=nil thentmpnode:=dxTreeList1.Add /添加根节点elsetmpnode:=n
11、ode.AddChild; /添加子节点pid:=FieldByName(id).AsInteger;tmpnode.Strings0:=IntToStr(pid);tmpnode.Strings1:=FieldByName(pid).AsString;tmpnode.Strings2:=FieldByName(aName).AsString;tmpnode.Strings3:=IntToStr(tmpnode.Level);tmpnode.ImageIndex:=tmpnode.Level;tmpnode.SelectedIndex:=tmpnode.Level;FillTree(tmpno
12、de,pid,con); /递归添加子节点Next; end;end;finallyADOQuery.Free;end;end;procedure TfrmTree.btnblClick(Sender: TObject); /遍历子节点varnode,tmpnode:TdxTreeListNode;i:Integer;str:string;beginif dxTreeList1.Count=0 then Exit;node:=dxTreeList1.FocusedNode; /获取焦点的节点for i:=0 to dxTreeList1.ColumnCount-1 dostr:=str+nod
13、e.Stringsi+;ShowMessage(str);tmpnode:=node.GetFirstChild; /获取第一个子节点while tmpnodeIdOK then Exit;if node.HasChildren then node.DeleteChildren; /删除子节点for i:=0 to dxTreeList1.ColumnCount-1 dobeginif dxTreeList1.Columnsi.Name=id thenbeginaid:=i;Break;endend;DelTree(StrToIntDef(node.Stringsaid,0),dm.con1)
14、;node.Free;end;end;procedure TfrmTree.DelTree(pid: Integer;con: TADOConnection); /删除数据库节点记录vartmpnode:TdxTreeListNode;ADOQuery:TADOQuery;beginADOQuery:=TADOQuery.Create(nil);tryADOQuery.Connection:=con;with ADOQuery dobeginClose;SQL.Clear;SQL.Text:=delete from DeviceTab where id=+Inttostr(pid); /删除当
15、前节点记录ExecSQL;end;ADOQuery.Close;ADOQuery.SQL.Clear;ADOQuery.SQL.Text:=select * from DeviceTab where pid=+Inttostr(pid); /查找子节点记录ADOQuery.Open;with ADOQuery dobeginFirst;while not Eof dobeginDelTree(FieldByName(id).AsInteger,con); /递归删除下级记录Next;end;end;finallyADOQuery.Free;end;end;procedure TfrmTree.
16、btnUpdateClick(Sender: TObject); 修改varnode:TdxTreeListNode;aname:string;i,j,k,id:Integer;IsExist:Boolean;beginIsExist:=False;if dxTreeList1.Count=0 thenbeginShowMessage(没有要修改的节点);exit;endelsebegindxTreeList1.BeginUpdate;node:=dxTreeList1.FocusedNode;for i:=0 to dxTreeList1.ColumnCount-1 dobeginif dx
17、TreeList1.Columnsi.Name=aName thenbeginj:=i;aname:=node.Stringsi;Break;endelse if dxTreeList1.Columnsi.Name=id thenbegink:=i;id:=StrToIntDef(node.Stringsi,0);end;end;if InputQuery(修改节点,请输入修改后的节点名 ,aname) thenbeginnode.Stringsj:=aname;UpdateTree(aname,id,dm.con1);/更新数据库记录end;end;dxTreeList1.EndUpdate
18、;end;procedure TfrmTree.UpdateTree(newname: string; id: Integer;con: TADOConnection);varqry:TADOQuery;beginqry :=TADOQuery.Create(nil);trywith qry dobeginConnection:=con;Close;SQL.Clear;SQL.Text:=update DeviceTab set aname=+Quotedstr(newname)+ where id=+Inttostr(id);ExecSQL;end;finallyqry.Free;end;e
19、nd;procedure TfrmTree.tmr1Timer(Sender: TObject); begintmr1.Enabled:=False;list:=TStringList.Create;dm.con1.GetFieldNames(DeviceTab,list); /获取表的字段名CreateColumn(dxTreeList1,list);CreateImageColumn(dxTreeList1,dm.il1);dxTreeList1.BeginUpdate;FillTree(nil,0,dm.con1);dxTreeList1.EndUpdate;end;procedure
20、TfrmTree.FormDestroy(Sender: TObject);beginlist.Free;if dm.con1.Connected thendm.con1.Close;end;function TFrmTree.GetCount(tree:TdxTreeList):integer; /获取总的节点数varnode:TdxTreeListNode;beginResult:=0;dxTreeList1.GotoFirst;node:=dxTreeList1.Items0;while nodenil dobegininc(Result);node:=node.GetNext;end;end;end.3.运行效果初始界面 添加节点后界面删除节点后界面修改节点后界面节点遍历( 部分截图)总结:dxTreelist 控件的属性和方法还有许多,功能比 TreeView 更丰富,同时操作也更复杂,它没有 treeview.items.count 属性获取节点总数,它的每一个节点都是一个 TdxTreeListNode 节点,操作每个节点时用 Strings 或 Values集合来赋值给节点的每一列,以后如果用到再深入研究一下,这个 Demo 足够一般的应用。