| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1213 人关注过本帖
标题:一个TreeView的问题
取消只看楼主 加入收藏
eqmaster
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-8-31
结帖率:0
收藏
已结贴  问题点数:0 回复次数:0 
一个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
快速回复:一个TreeView的问题
数据加载中...
 
   



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

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