标题:一个TreeView的问题
只看楼主
eqmaster
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-8-31
结帖率:0
已结贴  问题点数:0 回复次数:1 
一个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.
达人们帮着看下,谢了~
搜索更多相关主题的帖子: TreeView 
2009-08-31 16:45
dyfdb
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:24
专家分:162
注 册:2009-7-21
得分:20 
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;
===========================================================================
MyTree是一个类,不能直接使用,所以出错, 应该创建一个对象才能使用,改成这样:
procedure TForm1.FormCreate(Sender: TObject);
var
  ds: TDataSet;
  aTree:MyTree;
begin
  self.ADOTable1.Active := True;
  aTree:=MyTree.Create;
  TreeView1.Items := MyTree.MakeTree(self.ADOTable1);
  aTree.Free;
  //这下没错了
end;
=============================================================================
这个问题看起来是概念不清
Delphi编程是面向对象的,要清楚的理解类与对象的基本概念
类是关于对象的描述,是个抽象的概念,对象是类的实例,完全遵守类的规定
每一个具体的对象,都具有类描绘的属性和方法事件,这些描述就由类来完成
比方说,人类是个类,人才是具体的对象,
假如你要结婚,需要去找个具体的对象,而不是去找人类.人类只是关于人的基本属性的描述
参考下面的程序
var
  a: T人类  // 傻子
  b: T人类  // 警察
  c: T人类  // 小偷
  d: T狗类  // 野狗
begin
  c 偷了 a;
  b 抓了 c;
  d 咬了 b;
end;







[ 本帖最后由 dyfdb 于 2009-9-3 10:04 编辑 ]
2009-09-03 09:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-284294-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.170292 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved