W
wangchengwu
Unregistered / Unconfirmed
GUEST, unregistred user!
那位高手能给我实现以下树的添加、删除节点功能?
1。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DB, ADODB, Mask, DBCtrls, ExtCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
new: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure FormCreate(Sender: TObject);
procedure newClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function loadcode(treeDB:TADOTable):integer;
function getlevel(sformat,scode:string):integer;
{ Public declarations }
end;
var
Form1: TForm1;
tv:ttreenode;
const
scodeformat = '322222'; //科目代码结构
sfirstnodetxt = '科目代码'; //首节点显示的文字
implementation
{$R *.dfm}
function tform1.loadcode(treeDB:TADOTable):integer;
var
nowid,sname,showtxt:string;
i,level:integer;
mynode:array[0..6]of ttreenode;
begin
screen.cursor:=crhourglass;
level:=0;
with treeDB do
begin
try
if not active then open;
first;
TreeView1.items.clear;
mynode[level]:=TreeView1.items.add(TreeView1.topitem,sfirstnodetxt);
mynode[level].imageindex:=0;
mynode[level].selectedindex:=0;
while not eof do
begin
nowid:=trim(fieldbyname('acode').asstring);
showtxt:=nowid+'------------'+fieldbyname('aname').asstring;
level:=getlevel(scodeformat,nowid);
if level> 0 then
begin
mynode[level]:=TreeView1.items.addchild(mynode[level-1],showtxt);
mynode[level].imageindex:=1;
mynode[level].selectedindex:=2;
end;
next;
end;
finally
close;
end;
end;
mynode[0].expand(false);
screen.cursor:=crdefault;
end;
function tform1.getlevel
(sformat,scode:string):integer;
var i,level,ilen:integer;
begin
level:=-1;
ilen:=0;
if (sformat<>'')and(scode<>'')then
for i:=1 to length(sformat) do
begin
ilen:=ilen+strtoint(sformat);
if length(scode)=ilen then
begin
level:=i;
break;
end;
end;
result:=level;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with ADOTable1 do
begin
open;
indexfieldnames:='acode';
end;
loadcode(ADOTable1);
end;
2。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DB, ADODB, Mask, DBCtrls, ExtCtrls, Menus;
const
cTreeCodeFormat='122222';
cTreeMaxLevel=6;
cTreeRootTxt='单位字典';
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
Button1: TButton;
PopupMenu1: TPopupMenu;
Add1: TMenuItem;
Delete1: TMenuItem;
Edit1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Add1Click(Sender: TObject);
procedure Delete1Click(Sender: TObject);
procedure Edit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure LoadTree(treeDB:TADOTable);
procedure UpdateTree(curNode:TTreenode;nodeTxt:string;state:string);
function GetNodeLeveL(sFormat,sCode:string):integer;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetNodeLeveL(sFormat,scode:string):integer;
var i,level,iLen:integer;
begin
level:=-1;
iLen:=0;
if (sFormat<>'') and (sCode<>'') then
for i:=1 to length (sformat) do
begin
iLen:=iLen+StrToInt(sFormat);
if length(sCode)=iLen then
begin
level:=i;
break;
end;
end;
result:=level;
end;
procedure TForm1.LoadTree(treeDB:TADOTable);
var
curID,nodeTxt:string;
level:integer;
mynode:array[0..6] of TTreenode;
begin
TreeView1.Enabled:=true;
TreeView1.Items.clear;
level:=0;
TreeView1.items.clear;
mynode[level]:=TreeView1.Items.add(TreeView1.Topitem,cTreeRootTxt);
mynode[level].ImageIndex :=1;
with TreeDB DO
begin
try
if not Active then open;
first;
while not Eof do
begin
curID:=trim(FieldByName('code').Asstring);
nodeTxt:=curID+'-'+trim(FieldByName('name').Asstring);
level:=GetNodelevel(cTreeCodeFormat,curID);
if level>0 then
begin
mynode[level]:=TreeView1.items.addchild(mynode[level-1],nodeTxt);
mynode[level].imageIndex:=2;
end;
next;
end;
finally;
close;
end;
mynode[0].expand(true);
end;
end;
procedure TForm1.UpdateTree(curNode:TTreenode;nodeTxt:string;state:string);
begin
if state='add' then
begin
curNode:=TreeView1.items.addchild(curNode,nodeTxt);
curNode.imageIndex:=2;
end;
if state='del' then curNode.delete;
if state='edi' then curNode.text:=NodeTxt;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with ADOTable1 do
begin
open;
indexFieldNames:='code';
active:=true;
end;
LoadTree(ADOTable1);
end;
1。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DB, ADODB, Mask, DBCtrls, ExtCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
new: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure FormCreate(Sender: TObject);
procedure newClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function loadcode(treeDB:TADOTable):integer;
function getlevel(sformat,scode:string):integer;
{ Public declarations }
end;
var
Form1: TForm1;
tv:ttreenode;
const
scodeformat = '322222'; //科目代码结构
sfirstnodetxt = '科目代码'; //首节点显示的文字
implementation
{$R *.dfm}
function tform1.loadcode(treeDB:TADOTable):integer;
var
nowid,sname,showtxt:string;
i,level:integer;
mynode:array[0..6]of ttreenode;
begin
screen.cursor:=crhourglass;
level:=0;
with treeDB do
begin
try
if not active then open;
first;
TreeView1.items.clear;
mynode[level]:=TreeView1.items.add(TreeView1.topitem,sfirstnodetxt);
mynode[level].imageindex:=0;
mynode[level].selectedindex:=0;
while not eof do
begin
nowid:=trim(fieldbyname('acode').asstring);
showtxt:=nowid+'------------'+fieldbyname('aname').asstring;
level:=getlevel(scodeformat,nowid);
if level> 0 then
begin
mynode[level]:=TreeView1.items.addchild(mynode[level-1],showtxt);
mynode[level].imageindex:=1;
mynode[level].selectedindex:=2;
end;
next;
end;
finally
close;
end;
end;
mynode[0].expand(false);
screen.cursor:=crdefault;
end;
function tform1.getlevel
(sformat,scode:string):integer;
var i,level,ilen:integer;
begin
level:=-1;
ilen:=0;
if (sformat<>'')and(scode<>'')then
for i:=1 to length(sformat) do
begin
ilen:=ilen+strtoint(sformat);
if length(scode)=ilen then
begin
level:=i;
break;
end;
end;
result:=level;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with ADOTable1 do
begin
open;
indexfieldnames:='acode';
end;
loadcode(ADOTable1);
end;
2。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DB, ADODB, Mask, DBCtrls, ExtCtrls, Menus;
const
cTreeCodeFormat='122222';
cTreeMaxLevel=6;
cTreeRootTxt='单位字典';
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
Button1: TButton;
PopupMenu1: TPopupMenu;
Add1: TMenuItem;
Delete1: TMenuItem;
Edit1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Add1Click(Sender: TObject);
procedure Delete1Click(Sender: TObject);
procedure Edit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure LoadTree(treeDB:TADOTable);
procedure UpdateTree(curNode:TTreenode;nodeTxt:string;state:string);
function GetNodeLeveL(sFormat,sCode:string):integer;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetNodeLeveL(sFormat,scode:string):integer;
var i,level,iLen:integer;
begin
level:=-1;
iLen:=0;
if (sFormat<>'') and (sCode<>'') then
for i:=1 to length (sformat) do
begin
iLen:=iLen+StrToInt(sFormat);
if length(sCode)=iLen then
begin
level:=i;
break;
end;
end;
result:=level;
end;
procedure TForm1.LoadTree(treeDB:TADOTable);
var
curID,nodeTxt:string;
level:integer;
mynode:array[0..6] of TTreenode;
begin
TreeView1.Enabled:=true;
TreeView1.Items.clear;
level:=0;
TreeView1.items.clear;
mynode[level]:=TreeView1.Items.add(TreeView1.Topitem,cTreeRootTxt);
mynode[level].ImageIndex :=1;
with TreeDB DO
begin
try
if not Active then open;
first;
while not Eof do
begin
curID:=trim(FieldByName('code').Asstring);
nodeTxt:=curID+'-'+trim(FieldByName('name').Asstring);
level:=GetNodelevel(cTreeCodeFormat,curID);
if level>0 then
begin
mynode[level]:=TreeView1.items.addchild(mynode[level-1],nodeTxt);
mynode[level].imageIndex:=2;
end;
next;
end;
finally;
close;
end;
mynode[0].expand(true);
end;
end;
procedure TForm1.UpdateTree(curNode:TTreenode;nodeTxt:string;state:string);
begin
if state='add' then
begin
curNode:=TreeView1.items.addchild(curNode,nodeTxt);
curNode.imageIndex:=2;
end;
if state='del' then curNode.delete;
if state='edi' then curNode.text:=NodeTxt;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with ADOTable1 do
begin
open;
indexFieldNames:='code';
active:=true;
end;
LoadTree(ADOTable1);
end;