用数据表创建树

类别:Delphi 点击:0 评论:0 推荐:

数据库结构:
字段      类型
ID          整型     索引(无重复)
name      文本
father     整型



//tree初始化
procedure TForm1.FormActivate(Sender: TObject);
  var i:integer;
begin
  treeview1.Items.BeginUpdate;
  while not(adotable1.Eof) do
  begin
    if ADOTable1.FieldValues['father']=0 then
      treeview1.Items.addchild(treeview1.DropTarget,ADOTable1.FieldValues['name'])
    else
    begin
      i:=0;
      repeat
        if treeview1.Items.Item[i].Text = ADOTable1.Lookup('ID',ADOTable1.FieldValues['father'],'name') then
        begin
          treeview1.Items.AddChild(treeview1.Items.Item[i],ADOTable1.FieldValues['name']);
          break;
        end;
        i:=i+1;
      until i>adotable1.RecordCount;
    end;
    ADOTable1.next;
  end;
  treeview1.Items.EndUpdate;
end;

//tree添加
procedure TForm1.Button1Click(Sender: TObject);
  var i:integer;
begin
  adotable1.Last;
  i:=adotable1.FieldValues['ID'];
  if checkbox1.Checked=false then
  begin
    adotable1.InsertRecord([edit1.Text,adotable1.Lookup('name',treeview1.Selected.Text,'ID'),i+1]);
    treeview1.Items.AddChild(treeview1.Selected,edit1.Text);
  end
  else
  begin
    adotable1.InsertRecord([edit1.Text,0,i+1]);
    treeview1.Items.AddChild(treeview1.DropTarget,edit1.Text);
  end;
end;

//tree删除
procedure TForm1.Button3Click(Sender: TObject);
begin
  if treeview1.Selected.HasChildren then
  begin
    showmessage('含有子项,不能删除.');
    abort;
  end;
  begin
    adotable1.Locate('name',treeview1.Selected.Text,[loPartialKey]);
    adotable1.Delete;
    treeview1.Items.Delete(treeview1.Selected);
  end;
end;

//tree图标
procedure TForm1.TreeView1GetImageIndex(Sender: TObject; Node: TTreeNode);
begin
  if Node.Expanded then
    Node.ImageIndex := 1
  else
    Node.ImageIndex := 0
end;

//tree图标
procedure TForm1.TreeView1GetSelectedIndex(Sender: TObject;Node: TTreeNode);
begin
  node.SelectedIndex:=node.ImageIndex;
end;

//list初始化
procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
  var p:pointer;
begin
  listview1.Items.Clear;
  if adotable2.Locate('class',adotable1.Lookup('name',treeview1.Selected.Text,'ID'),[loPartialKey])=true then
  begin
    p:=listview1.Items.Add;
    listview1.Items.Item[listview1.Items.IndexOf(p)].Caption:=adotable2.FieldValues['name'];
    listview1.Items.Item[listview1.Items.IndexOf(p)].SubItems.Add(adotable2.FieldValues['url']);
  end;
end;

//list添加
procedure TForm1.Button2Click(Sender: TObject);
  var p:pointer;
begin
  p:=listview1.Items.Add;
  listview1.Items.Item[listview1.Items.IndexOf(p)].Caption:=edit2.Text;
  listview1.Items.Item[listview1.Items.IndexOf(p)].SubItems.Add(edit3.Text);
  adotable2.InsertRecord([adotable1.Lookup('name',treeview1.Selected.Text,'ID'),edit2.Text,edit3.Text]);
end;

//list删除
procedure TForm1.Button4Click(Sender: TObject);
begin
  adotable2.Locate('name',listview1.Selected.Caption,[loPartialKey]);
  adotable2.Delete;
  listview1.Selected.Delete;
end;

本文地址:http://com.8s8s.com/it/it4696.htm