sz_ztcls.pas
资源名称:挡案管理系统.rar [点击查看]
上传用户:szruida
上传日期:2007-06-01
资源大小:6518k
文件大小:15k
源码类别:
Delphi控件源码
开发平台:
Delphi
- unit sz_ztcls;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, Menus, ActnList, DB, XPBarMenu, Grids, DBGridEh, StdCtrls,
- ComCtrls, ExtCtrls, ToolWin, Mask, DBCtrls, DBClient;
- type
- Tsz_ztcl = class(TForm)
- CoolBar1: TCoolBar;
- ToolBar1: TToolBar;
- add1: TToolButton;
- xiugai: TToolButton;
- dele1: TToolButton;
- save1: TToolButton;
- refresh1: TToolButton;
- ToolButton3: TToolButton;
- Panel1: TPanel;
- Splitter1: TSplitter;
- Panel2: TPanel;
- Tree: TTreeView;
- Panel3: TPanel;
- Splitter2: TSplitter;
- Panel4: TPanel;
- Label8: TLabel;
- Label9: TLabel;
- Label10: TLabel;
- Edit1: TEdit;
- Panel5: TPanel;
- GroupBox2: TGroupBox;
- DBGridEh1: TDBGridEh;
- XPBarMenu1: TXPBarMenu;
- DataSource1: TDataSource;
- ActionList1: TActionList;
- Add: TAction;
- Edit: TAction;
- Delete: TAction;
- Save: TAction;
- Refresh: TAction;
- Print: TAction;
- Quit: TAction;
- PopupMenu1: TPopupMenu;
- A1: TMenuItem;
- D1: TMenuItem;
- E1: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- Label1: TLabel;
- Label2: TLabel;
- Label4: TLabel;
- dbEdit2: TEdit;
- dbEdit4: TEdit;
- DbEdit1: TEdit;
- ClientDataSet1: TClientDataSet;
- procedure N5Click(Sender: TObject);
- procedure N6Click(Sender: TObject);
- procedure QuitExecute(Sender: TObject);
- procedure AddExecute(Sender: TObject);
- procedure EditExecute(Sender: TObject);
- procedure DeleteExecute(Sender: TObject);
- procedure SaveExecute(Sender: TObject);
- procedure RefreshExecute(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure TreeClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure DBGridEh1CellClick(Column: TColumnEh);
- procedure DbEdit1KeyPress(Sender: TObject; var Key: Char);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure loadTree(treeDb:TClientDataSet);//初始化树
- procedure BtnState();//增,改时的按钮变化
- procedure SaveData(TreeView:TtreeView;dbedit1,dbedit2:Tedit);//保存数据
- end;
- var
- sz_ztcl: Tsz_ztcl;
- gnode:TtreeNode;//当前节点
- lbjs_sm:string;//类别级数
- lb_dmgz:string;//代码规则
- dsState:string;//增删改的状态
- implementation
- uses DMs,u_public;
- {$R *.dfm}
- procedure Tsz_ztcl.BtnState();
- begin
- if add1.Enabled=true or xiugai.Enabled=true then
- begin
- add1.Enabled:=false;A1.Enabled:=false;
- xiugai.Enabled:=false;E1.Enabled:=false;
- dele1.Enabled:=false;D1.Enabled:=false;
- save1.Enabled:=true;
- refresh1.Enabled:=true;
- end
- else
- begin
- add1.Enabled:=true;A1.Enabled:=true;
- xiugai.Enabled:=true;E1.Enabled:=true;
- dele1.Enabled:=true;D1.Enabled:=true;
- save1.Enabled:=false;
- refresh1.Enabled:=true;
- end;
- end;
- //***********************************************
- procedure Tsz_ztcl.SaveData(TreeView:TtreeView;dbedit1,dbedit2:Tedit);//保存数据
- var
- curnode:TTreenode;//已选树节点
- Pnodetxt,nodetxt:string;//Pnodetxt:取出树上级节点节点的代码;nodetxt:要在树节点上增加的值
- n:integer;//取出'-'在已选树节点中的位置
- begin
- dm.sz_ztcl.edit;
- n:=pos('-',TreeView.Selected.Text)-1;
- Pnodetxt:=copy(TreeView.Selected.Text,1,n);
- nodetxt:=Pnodetxt+dbedit1.Text+'-'+dbedit2.Text;//现在节点等于原来的加上现在的
- //增加数据到节点
- dm.sz_ztcl.FieldByName('ztcl_dm').AsString:=Pnodetxt+dbedit1.Text;
- dm.sz_ztcl.FieldByName('ztcl_mc').AsString:=dbedit2.Text;
- dm.sz_ztcl.FieldByName('ztcl_js').AsInteger:=tree.Selected.Level;
- dm.sz_ztcl.FieldByName('ztcl_sjdm').AsString:=Pnodetxt;
- //给数组赋值
- dm.sz_ztcl.ApplyUpdates(0);
- application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
- curnode:=TreeView.Items.AddChild(tree.Selected,nodetxt);//增加新的节点
- curnode.ImageIndex:=0;
- curnode.SelectedIndex:=1;
- //-------按钮状态
- end;
- //**************************************************
- procedure Tsz_ztcl.loadTree(treeDb:TClientDataSet);//初始化树
- var
- nodeId,nodeTxt:string;
- SCodeFormat,SFirstNodeTxt:string;
- level:integer;
- mynode:array[0..6] of Ttreenode;
- begin
- //初始化变量
- SCodeFormat:=lbjs_sm ; //库位代码结构
- SFirstNodeTxt:= '主题词类别'; //首节点显示的文字
- Screen.Cursor:=crHourGlass;
- Tree.enabled:=true;
- tree.Items.Clear;
- level:=0;
- //设置根节点
- mynode[level]:=tree.Items.Add(tree.TopItem,'库位');
- mynode[level].ImageIndex:=0;
- mynode[level].SelectedIndex:=1;
- //遍历数据表,利用编码字段记录排序规律,依次添加树节点
- with treeDB do
- begin
- try
- if not active then open;
- first;
- while not treedb.Eof do
- begin
- nodeId:=trim(fieldbyname('ztcl_dm').AsString);
- nodeTxt:=nodeId+'-'+trim(fieldbyname('ztcl_mc').AsString);
- level:=GetNodeLevel(SCodeFormat,nodeId);//获得节点层树
- //再这里返回代码的层次数
- if level>0 then
- begin
- //增加下一节点时,用添加子节点的方法可以轻松实现节点的层次关系
- //注意,这里的父节点是用当前节点的上一级节点mynode[level-1]
- mynode[level]:=tree.Items.AddChild(mynode[level-1],nodeTxt);
- mynode[level].ImageIndex:=0;
- mynode[level].SelectedIndex:=1;
- end;//end if
- next;//下一条纪录
- end;//end while
- finally;
- close;
- end;
- mynode[0].Expand(false);
- screen.Cursor:=crdefault;
- end;//end with
- end;
- procedure Tsz_ztcl.N5Click(Sender: TObject);
- begin
- tree.Selected.expand(true);
- end;
- procedure Tsz_ztcl.N6Click(Sender: TObject);
- begin
- tree.Selected.Collapse(true);
- end;
- procedure Tsz_ztcl.QuitExecute(Sender: TObject);
- begin
- close;
- end;
- procedure Tsz_ztcl.AddExecute(Sender: TObject);
- begin
- if tree.Selected=nil then
- exit
- else
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl where ztcl_dm='+''''+''''+'';
- dm.sz_ztcl.Open;//增加
- if tree.Selected.level=length(trim(lbjs_sm)) then
- begin
- application.MessageBox('已经到达最底层的节点,不能再增加','提示',mb_ok+mb_iconinformation);
- exit;
- end;
- dbedit1.SetFocus;
- dbedit4.Text:=IntToStr(tree.Selected.level);
- BtnState();//调用按钮的状态参数
- dsState:='add';
- dbedit1.Text:='';
- dbedit2.Text:='';
- end;
- procedure Tsz_ztcl.EditExecute(Sender: TObject);
- var s1,s2:string;
- Epos:integer;
- begin
- if tree.selected=nil then
- exit
- else
- if tree.Items.Count<>0 then
- if Tree.Items[0].HasChildren<>false then
- begin
- BtnState();//调用按钮的状态参数
- s1:=Tree.Selected.Text;
- Epos:=pos('-',s1);//取得'-'在节点名中的位置
- s2:=copy(s1,0,Epos-1);//
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl where ztcl_dm='+''''+s2+''''+'';
- dm.sz_ztcl.Open;
- dbedit1.Text:=dm.sz_ztcl.fieldbyname('ztcl_dm').AsString;
- Dbedit1.Enabled:=false;
- DBEdit1.Color:=cl3DLight;
- dm.sz_ztcl.edit;
- end
- else
- begin
- application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
- exit;
- end;
- end;
- procedure Tsz_ztcl.DeleteExecute(Sender: TObject);
- var
- epos:integer;
- s1,s2:string;
- begin
- if tree.Selected=nil then
- exit
- else
- if tree.Items[0].HasChildren then
- if tree.Selected.HasChildren=true then
- begin
- application.messagebox('该部门包含下级,不能删除','提示',MB_OKCANCEL+mb_iconinformation );
- end
- else
- begin
- if application.messagebox('您确实要删除当前纪录吗?','提示',MB_OKCANCEL+mb_iconquestion )=IDOK then
- begin
- s1:=Tree.Selected.Text;
- Epos:=pos('-',s1);//取得'-'在节点名中的位置
- s2:=copy(s1,0,Epos-1);//
- tree.Selected.Delete;//删除树形表中数据
- dbedit1.Text:='';dbedit2.text:='';
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='delete from sz_ztcl where ztcl_dm='+''''+s2+''''+'';
- dm.sz_ztcl.execute;
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl';
- dm.sz_ztcl.Open;
- //删除表中数据
- Clientdataset1.Close;
- Clientdataset1.CommandText:='select * from sz_ztcl';
- Clientdataset1.Open;//
- if Clientdataset1.recordcount=0 then
- begin
- dele1.Enabled:=false;
- xiugai.Enabled:=false;
- end;
- end;
- end;
- end;
- procedure Tsz_ztcl.SaveExecute(Sender: TObject);
- var
- s_sj,yanzheng:string;//yangzheng查找数据库中是否有重复记录存在
- Pnodetxt:string;//pnodetxt:父节点值
- i,n,n_sj:integer;
- hint:string;
- s1,s2:string;
- Epos:integer;
- begin
- if dbedit1.Text='' then
- begin
- application.MessageBox('主题词代码不能为空','提示',mb_ok+mb_iconinformation);
- DbEdit1.SetFocus;
- exit;
- end;
- if dbedit2.Text='' then
- begin
- application.MessageBox('主题词名称不能为空','提示',mb_ok+mb_iconinformation);
- DbEdit2.SetFocus;
- exit;
- end;
- //----------------------------------------------------------------------
- //判断是增加还是修改*****************************************************
- if dbedit1.Enabled=true then
- begin
- if tree.Selected.Level=length(lbjs_sm) then
- begin
- application.MessageBox('已经到达最底层节点'#13#10'不能增加','提示',mb_ok+mb_iconinformation);
- exit;
- end else
- for i:=0 to tree.Selected.Level do
- begin
- if tree.Selected.Level=i then
- begin//&&&&
- if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,i+1,1)) then
- begin
- hint:='请输入'+copy(lbjs_sm,i+1,1)+'位代码';
- application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
- exit;
- end
- else//
- begin
- s1:=Tree.Selected.Text;
- Epos:=pos('-',s1);//取得'-'在节点名中的位置
- s2:=copy(s1,0,Epos-1);//
- yanzheng:=s2+dbedit1.Text;
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl where ztcl_dm='+''''+yanzheng+''''+'';
- dm.sz_ztcl.Open;
- if dm.sz_ztcl.FieldByName('ztcl_dm').AsString<>'' then
- begin
- application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
- BtnState();
- exit;
- end
- else
- begin
- SaveData(tree,dbedit1,dbedit2);//调用保存过程
- //refresh1.Click;
- BtnState();//调用按钮的状态参数
- break;//成功则退出循环
- end;
- end;//end if 0
- end//&&&&
- else continue;
- end;//end for
- end//end 结束增加
- else
- begin //修改后的情况
- dm.sz_ztcl.Edit;
- Tree.Selected.EditText;//修改节点
- Tree.Selected.Text:=Dbedit1.text+'-'+DbEdit2.Text;
- tree.Selected.ImageIndex:=0;
- tree.Selected.SelectedIndex:=1;
- //增加数据到节点
- dm.sz_ztcl.FieldByName('ztcl_dm').AsString:=dbedit1.Text;//代码
- dm.sz_ztcl.FieldByName('ztcl_mc').AsString:=dbedit2.Text;//名称
- dm.sz_ztcl.FieldByName('ztcl_js').AsInteger:=StrToInt(dbEdit4.text);//级数
- //dm.sz_ztcl.FieldByName('ztcl_sjdm').AsString:=dbEdit3.Text;//上级类别代码
- //给数组赋值
- dm.sz_ztcl.ApplyUpdates(0);
- application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
- BtnState();
- dbedit1.Enabled:=true;
- dbedit1.Color:=clwindow;
- end;
- end;
- procedure Tsz_ztcl.RefreshExecute(Sender: TObject);
- begin
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl';
- dm.sz_ztcl.Open;
- if dm.sz_ztcl.recordcount<>0 then
- begin
- Add1.Enabled:=true;xiugai.Enabled:=true;dele1.Enabled:=true;save1.Enabled:=true;
- A1.Enabled:=true;E1.Enabled:=true;D1.Enabled:=true;
- end
- else
- begin
- Add1.Enabled:=true;xiugai.Enabled:=false;dele1.Enabled:=false;save1.Enabled:=false;
- A1.Enabled:=true;E1.Enabled:=false;D1.Enabled:=false;
- end;
- end;
- procedure Tsz_ztcl.FormShow(Sender: TObject);
- begin
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl';
- dm.sz_ztcl.Open;
- dm.xt_lbjs.Close;
- dm.xt_lbjs.CommandText:='select lbjs_sm,lb_dmgz from xt_lbjs where lb_dm='+''''+'sz_ztcl '+''''+'';
- dm.xt_lbjs.Open;
- lbjs_sm:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
- lb_dmgz:=dm.xt_lbjs.fieldbyname('lb_dmgz').AsString;
- edit1.Text:=lb_dmgz;
- //-----------------------------------------------
- loadTree(dm.sz_ztcl);//初始化树
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl';
- dm.sz_ztcl.Open;
- end;
- procedure Tsz_ztcl.TreeClick(Sender: TObject);
- var
- s1,s2:string;
- i,Epos:integer;
- begin
- if tree.selected=nil then
- exit
- else
- if Tree.Items[0].HasChildren<>false then
- begin //选中和不选中的图片显示
- for i:=0 to Tree.items.count-1 do
- begin
- Tree.Items[i].ImageIndex:=0;
- Tree.Items[i].SelectedIndex:=1;
- s1:=Tree.Selected.Text;
- Epos:=pos('-',s1);
- s2:=copy(s1,0,Epos-1);
- dm.sz_ztcl.Close;
- dm.sz_ztcl.CommandText:='select * from sz_ztcl where ztcl_dm='+''''+s2+''''+'';
- dm.sz_ztcl.Open;
- dbedit1.Text:=dm.sz_ztcl.fieldbyname('ztcl_dm').AsString;
- dbedit2.Text:=dm.sz_ztcl.fieldbyname('ztcl_mc').AsString;
- //dbedit3.Text:=dm.sz_ztcl.fieldbyname('ztcl_sjdm').AsString;
- dbedit4.Text:=dm.sz_ztcl.fieldbyname('ztcl_js').AsString;
- end;
- end;
- end;
- procedure Tsz_ztcl.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- dm.sz_ztcl.Active:=false;
- dm.xt_lbjs.Active:=false;
- action:=cafree;
- sz_ztcl:=nil;
- Tree.Free;
- end;
- procedure Tsz_ztcl.DBGridEh1CellClick(Column: TColumnEh);
- var
- s1,s2:string;
- i:integer;
- begin//--1单击每一行时,使树形控件也变化
- s1:=DBGridEh1.Columns[0].DisplayText;
- s2:=DBGridEh1.Columns[1].DisplayText;
- dm.sz_ztcl.Locate('ztcl_dm','+''''+s1+''''+',[]);
- //过滤要搜索的数据
- dbedit1.Text:=dm.sz_ztcl.fieldbyname('ztcl_dm').AsString;
- dbedit2.Text:=dm.sz_ztcl.fieldbyname('ztcl_mc').AsString;
- //dbedit3.Text:=dm.sz_ztcl.fieldbyname('ztcl_sjdm').AsString;
- dbedit4.Text:=dm.sz_ztcl.fieldbyname('ztcl_js').AsString;
- if Tree.Items[0].HasChildren<>false then
- begin//end if
- for i:=0 to Tree.items.count-1 do
- if Tree.Items[i].Text=s1+'-'+s2 then
- begin
- Tree.Items[i].Selected:=true;
- break;
- end;//end for
- end;//end if
- end;//--1
- procedure Tsz_ztcl.DbEdit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if key=#13 then
- begin
- key:=#0;
- SelectNext(ActiveControl,true,true);
- end;
- end;
- end.