以下是我的一个应用,你不妨参考以下
const
iCodeLen=6;//代码长度
sCodeFormat='1121';//代码级长
{$R *.dfm}
function TFd_sys_dept.f_appendcode():Boolean;
begin
with DS_Deptdo
begin
if DataSet.Active then
begin
DataSet.Insert;
DataSet['bmbh']:=Trim(Edit_id.Text);
DataSet['bmmc']:=Trim(Edit_code.Text);
DataSet.Post;
DataSet.Close;
MisData.P_CloseTable;
MisData.P_OpenTable;
Fd_sys_dept.LoadCode(AQ_Dept);
Result:=True;
exit;
end
else
Result:=False;
end;
end;
function TFd_sys_dept.f_updatecode():Boolean;
begin
with DS_Deptdo
begin
if DataSet.Active then
begin
DataSet.Edit;
DataSet['bmbh']:=Trim(Edit_id.Text);
DataSet['bmmc']:=Trim(Edit_code.Text);
DataSet.Post;
DataSet.Close;
MisData.P_CloseTable;
MisData.P_OpenTable;
Fd_sys_dept.LoadCode(AQ_Dept);
Result:=True;
exit;
end
else
Result:=False;
end;
end;
function TFd_sys_dept.f_deletecode():Boolean;
begin
with DS_Deptdo
begin
if DataSet.Active then
begin
DataSet.Delete;
DataSet.Close;
MisData.P_CloseTable;
MisData.P_OpenTable;
Fd_sys_dept.LoadCode(AQ_Dept);
Result:=True;
exit;
end
else
Result:=False;
end;
end;
function TFd_sys_dept.LoadCode(crAQuery: TADOQuery):Integer;
var
Level,i :Integer;
NowId,ShowTxt :String;
sid,scode :String;
MyNode:array[0..6] of TTreeNode;
begin
Screen.Cursor:=crHourGlass;
Level:=0;
try
if not crAQuery.Active then
crAQuery.Open;
if (crAQuery.RecordCount = 0) then
begin
MessageDlg('打开参数库失败!', mtInformation,[mbOk], 0);
exit;
end;
TreeView1.Items.Clear;
// remove any existing nodes
//以下是增加第一项
MyNode[Level]:=TreeView1.Items.Add(TreeView1.TopItem, '部门代码');
MyNode[Level].ImageIndex:=0;
MyNode[Level].SelectedIndex:=0;
//以上是增加第一项
crAQuery.First;
while (not crAQuery.Eof)do
begin
sid:=crAQuery['bmbh'];
//此处不能使用AsString;
scode:=crAQuery['bmmc'];
NowId:=Trim(sid);
if Length(sid)<iCodeLen then
begin
i:=iCodeLen-Length(sid);
sid:=sid+StringOfChar(' ',i);
ShowTxt:=sid+scode;
Level:=MyGetLevel(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;
//以上是增加子项
crAQuery.Next;
end;
end;
finally
crAQuery.First;
end;
MyNode[0].Expand(False);
TreeView1.FullExpand;
Screen.Cursor:=crDefault;
end;
function TFd_sys_dept.MyGetLevel(sFormat,sCode:String):Integer;
var
i,Level,iLen:Integer;
begin
Level:=-1;
iLen:=0;
if ((Trim(sFormat)<>'') and (Trim(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 TFd_sys_dept.FormCreate(Sender: TObject);
begin
LoadCode(AQ_Dept);
end;
procedure TFd_sys_dept.TreeView1Change(Sender: TObject;
Node: TTreeNode);
var
sselect:String;
begin
sselect:=Trim(TreeView1.Selected.Text);
if sselect = '部门代码' then
exit;
StatusBar1.SimpleText := sselect;
with AQ_Deptdo
begin
Filtered := false;
if (Length(LeftStr(sselect,iCodeLen)) = 1) then
Filter := ''
else
Filter := 'bmbh = ' + QuotedStr(Trim(LeftStr(sselect,iCodeLen)));
Filtered := true;
Edit_id.Text:=DS_Dept.DataSet['bmbh'];
Edit_code.Text:=DS_Dept.DataSet['bmmc'];
end;
end;
procedure TFd_sys_dept.BitBtn4Click(Sender: TObject);
begin
Close;
end;
procedure TFd_sys_dept.FormClose(Sender: TObject;var Action: TCloseAction);
begin
Fd_sys_dept:=nil;
Action := caFree;
end;