Delphi TreeView 使用方法。连接access数据库
TreeView连接了一个access数据库,并且可以通过两个DBEdit1,对TreeView进行操作,同时也对access数据库的数据进行操作。这里有图(http:/...
TreeView 连接了一个access数据库,并且可以通过两个DBEdit1,对TreeView 进行操作,同时也对access数据库的数据进行操作。
这里有图 (http://wenwen.soso.com/z/q210884984.htm)
请帮忙,谢谢。
把源码发下,也把程序发给下 443030595@qq.com 展开
这里有图 (http://wenwen.soso.com/z/q210884984.htm)
请帮忙,谢谢。
把源码发下,也把程序发给下 443030595@qq.com 展开
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB, Mask, DBCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
TempQy: TADOQuery;
TempDs: TDataSource;
Button4: TButton;
procedure InitList;
procedure FormShow(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
Function FindMaxCode(CodLength:Integer;ParentCode:string):string;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
type myint = ^string;
var
pt : myint;
implementation
{$R *.dfm}
//查找最大的代码
Function TForm1.FindMaxCode(CodLength:Integer;ParentCode:string):string;
var
SqlStr:string;
begin
result:='';
SqlStr:='select max(DepartmentCode) as MaxCode from CodeDepartment';
SqlStr:=SqlStr+' where len(DepartmentCode)='+#39+inttostr(CodLength)+#39;
SqlStr:=SqlStr+' and ParentCode='+#39+ParentCode+#39;
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('查找最大的代码失败!','提示',16);
exit;
end;
result:=TempQy.FieldByName('MaxCode').AsString;
end;
//初始化列表
procedure TForm1.InitList;
var node : ttreenode;
sbm,sbm1,smc : string;
SqlStr : string;
s : integer;
begin
node := nil;
sbm := '21';
//--读取列表
SqlStr:='select DepartmentCode,DepartmentName from CodeDepartment';
SqlStr:=SqlStr+' order by DepartmentCode';
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('与服务器连接失败,无法进入业务处理!','提示',16);
exit;
end;
TempQy.First;
TreeView1.Items.Clear;
while not TempQy.Eof do //循环数据表
begin
sbm1 := TempQy.FieldByName('DepartmentCode').asstring;
smc := TempQy.FieldByName('DepartmentName').asstring;
new(pt);
pt^ := TempQy.fieldbyname('DepartmentCode').asstring;
if length(sbm1) = 2 then //添加根结点
node := TreeView1.Items.AddObject(nil,smc,pt)
else if length(sbm1) = length(sbm) then //同级结点
node := TreeView1.Items.AddObject(node,smc,pt)
else if length(sbm1) > length(sbm) then //下级结点
node := TreeView1.Items.AddChildObject(node,smc,pt)
else if length(sbm1) < length(sbm) then //查找上一个结点
begin
s := length(sbm);
while length(sbm1) <> s do
begin
s := s - 2;
node := node.Parent;
end;
node := TreeView1.Items.AddObject(node,smc,pt);
end;
node.ImageIndex := 1;
sbm := sbm1;
TempQy.Next;
end;
if TreeView1.Items.GetFirstNode <> nil then
TreeView1.Items.GetFirstNode.Selected := true;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ADOQuery1.close;
ADOQuery1.Open;
InitList;
end;
//选中定位
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
s1 : string;
begin
s1 := myint(TreeView1.Selected.Data)^;
ADOQuery1.Locate('DepartmentCode',s1,[]);
end;
//增加根结点
procedure TForm1.Button1Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
CodLength:=2;
ParentCode:='0';
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength,ParentCode))+1);
if length(DepartmentCode)=1 then
DepartmentCode:='0'+ DepartmentCode;
ADOQuery1.Append;
ADOQuery1.Edit;
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;
//保存
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOQuery1.Post;
InitList;
end;
//增加结点
procedure TForm1.Button2Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
if length(ADOQuery1.FieldByName('DepartmentCode').AsString)=6 then
begin
showmessage('本系统只支持三级目录,不能在三级目录下增加结点!');
exit;
end;
CodLength:=length(ADOQuery1.FieldByName('DepartmentCode').AsString);
ParentCode:=ADOQuery1.FieldByName('DepartmentCode').AsString;
ADOQuery1.Append;
ADOQuery1.Edit;
if FindMaxCode(CodLength+2,ParentCode)='' then
DepartmentCode:=ParentCode+'01'
else
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength+2,ParentCode))+1);
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;
end.
DepartmentCode DepartmentName ParentCode
01 单位一 0
0101 部门一 01
010101 科室一 0101
010102 科室二 0101
0102 部门二 01
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB, Mask, DBCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
TempQy: TADOQuery;
TempDs: TDataSource;
Button4: TButton;
procedure InitList;
procedure FormShow(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
Function FindMaxCode(CodLength:Integer;ParentCode:string):string;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
type myint = ^string;
var
pt : myint;
implementation
{$R *.dfm}
//查找最大的代码
Function TForm1.FindMaxCode(CodLength:Integer;ParentCode:string):string;
var
SqlStr:string;
begin
result:='';
SqlStr:='select max(DepartmentCode) as MaxCode from CodeDepartment';
SqlStr:=SqlStr+' where len(DepartmentCode)='+#39+inttostr(CodLength)+#39;
SqlStr:=SqlStr+' and ParentCode='+#39+ParentCode+#39;
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('查找最大的代码失败!','提示',16);
exit;
end;
result:=TempQy.FieldByName('MaxCode').AsString;
end;
//初始化列表
procedure TForm1.InitList;
var node : ttreenode;
sbm,sbm1,smc : string;
SqlStr : string;
s : integer;
begin
node := nil;
sbm := '21';
//--读取列表
SqlStr:='select DepartmentCode,DepartmentName from CodeDepartment';
SqlStr:=SqlStr+' order by DepartmentCode';
try
TempQy.Close;
TempQy.SQL.Clear;
TempQy.SQL.Text:=SqlStr;
TempQy.Open;
except
APPLICATION.MessageBox ('与服务器连接失败,无法进入业务处理!','提示',16);
exit;
end;
TempQy.First;
TreeView1.Items.Clear;
while not TempQy.Eof do //循环数据表
begin
sbm1 := TempQy.FieldByName('DepartmentCode').asstring;
smc := TempQy.FieldByName('DepartmentName').asstring;
new(pt);
pt^ := TempQy.fieldbyname('DepartmentCode').asstring;
if length(sbm1) = 2 then //添加根结点
node := TreeView1.Items.AddObject(nil,smc,pt)
else if length(sbm1) = length(sbm) then //同级结点
node := TreeView1.Items.AddObject(node,smc,pt)
else if length(sbm1) > length(sbm) then //下级结点
node := TreeView1.Items.AddChildObject(node,smc,pt)
else if length(sbm1) < length(sbm) then //查找上一个结点
begin
s := length(sbm);
while length(sbm1) <> s do
begin
s := s - 2;
node := node.Parent;
end;
node := TreeView1.Items.AddObject(node,smc,pt);
end;
node.ImageIndex := 1;
sbm := sbm1;
TempQy.Next;
end;
if TreeView1.Items.GetFirstNode <> nil then
TreeView1.Items.GetFirstNode.Selected := true;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ADOQuery1.close;
ADOQuery1.Open;
InitList;
end;
//选中定位
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
s1 : string;
begin
s1 := myint(TreeView1.Selected.Data)^;
ADOQuery1.Locate('DepartmentCode',s1,[]);
end;
//增加根结点
procedure TForm1.Button1Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
CodLength:=2;
ParentCode:='0';
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength,ParentCode))+1);
if length(DepartmentCode)=1 then
DepartmentCode:='0'+ DepartmentCode;
ADOQuery1.Append;
ADOQuery1.Edit;
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;
//保存
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOQuery1.Post;
InitList;
end;
//增加结点
procedure TForm1.Button2Click(Sender: TObject);
var
CodLength:Integer;
ParentCode,DepartmentCode:string;
begin
if length(ADOQuery1.FieldByName('DepartmentCode').AsString)=6 then
begin
showmessage('本系统只支持三级目录,不能在三级目录下增加结点!');
exit;
end;
CodLength:=length(ADOQuery1.FieldByName('DepartmentCode').AsString);
ParentCode:=ADOQuery1.FieldByName('DepartmentCode').AsString;
ADOQuery1.Append;
ADOQuery1.Edit;
if FindMaxCode(CodLength+2,ParentCode)='' then
DepartmentCode:=ParentCode+'01'
else
DepartmentCode:=inttostr(strtoint(FindMaxCode(CodLength+2,ParentCode))+1);
ADOQuery1.FieldByName('DepartmentCode').AsString :=DepartmentCode;
ADOQuery1.FieldByName('ParentCode').AsString :=ParentCode;
end;
end.
DepartmentCode DepartmentName ParentCode
01 单位一 0
0101 部门一 01
010101 科室一 0101
010102 科室二 0101
0102 部门二 01
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询