一个TreeView的问题
unit Unit1;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB, DBTables, Grids, DBGrids, FMTBcd, DBClient,
SqlExpr;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
ADOTable1: TADOTable;
DBGrid1: TDBGrid;
Table1: TTable;
SQLQuery1: TSQLQuery;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
ds: TDataSet;
begin
self.ADOTable1.Active := True;
TreeView1.Items := MyTree.MakeTree(self.ADOTable1);
//出现错误:this form of method call only allowed for class methods
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ComCtrls;
type
mytree = class(Tobject)
//MyTreeNode: TTreeNode;
function MakeTree(tablerecordset: Tadotable): ttreenodes;
procedure addtreenode(tablerecordset: tadotable; TreeView: ttreeview);
end;
implementation
var
List: TStringList;
procedure mytree.addtreenode(tablerecordset: Tadotable; TreeView: ttreeview);
var
MyTreeView: TTreeView;
Node: TTreeNode;
index: integer;
begin
MyTreeView.Items.BeginUpdate;
List.Clear; //清空list(TStringList)
MyTreeView.items.clear; //清空 TreeView
if (tablerecordset.Fields[1].value = 0) then
Node := MyTreeView.Items.AddChildObject(nil,
tablerecordset.Fields[2].value, nil)
else
begin
Index := List.IndexOf(tablerecordset.Fields[1].value);
Node := TreeView.Items.AddChildObjectFirst(TTreeNode(List.Objects[Index]),
Tablerecordset.Fields[2].value, nil);
//增加子节点,并将本节点所对应的记录标签数据放到节点所提供的附加数据中
end;
List.AddObject(tablerecordset.Fields[0].value, Node);
end;
function mytree.MakeTree(tablerecordset: Tadotable): TTreeNodes;
var
treeview: ttreeview;
i: integer;
begin
i := 0;
TreeView.Items.BeginUpdate; //禁止刷新以快速显示,与EndUpdate成对使用
list.Clear; //清空list(TStringList)
TreeView.items.clear; //清空 TreeView
TreeView.Items.Clear; //清空 TreeView
list.Clear; //清空list(TStringList)
List.Sorted := True;
//treeview.Items.Clear; //增加根节点
//Rootnode := Treeview.Items.Add(nil, '全部');
while i < tablerecordset.RecordCount - 1 do //如果不为空开始依次增加所有节点
begin
AddTreeNode(tablerecordset, TreeView); //依次增加所有的节点
i := i + 1; //查询下一个PID
showmessage(inttostr(tablerecordset.recordcount));
end;
TreeView.Items.EndUpdate; //使其他控件有效
//recordset.EnableControls;
if TreeView.Items.Count < 1 then exit;
TreeView.Select(Treeview.Items.Item[0]); //打开时焦点落在Item[0]上,即ID=0的条目上编号
//TreeView.SetFocus;
result := treeview.Items;
end;
initialization //初始化List(TStringList)
List := TStringList.Create;
finalization
List.Free; //释放List
end.
达人们帮着看下,谢了~