程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 用數據表創建樹

用數據表創建樹

編輯:Delphi

  數據庫結構:
  字段      類型
  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;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved