sz_bms.pas
资源名称:挡案管理系统.rar [点击查看]
上传用户:szruida
上传日期:2007-06-01
资源大小:6518k
文件大小:14k
源码类别:
Delphi控件源码
开发平台:
Delphi
- unit sz_bms;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, Menus, ActnList, DB, XPBarMenu, Grids, DBGrids, StdCtrls, Mask,
- DBCtrls, ComCtrls, ExtCtrls, ToolWin, DBGridEh, DBClient, DBCtrlsEh;
- type
- Tsz_bm = class(TForm)
- CoolBar1: TCoolBar;
- ToolBar1: TToolBar;
- add1: TToolButton;
- xiugai: TToolButton;
- dele1: TToolButton;
- save1: TToolButton;
- refresh1: TToolButton;
- ToolButton3: TToolButton;
- Panel1: TPanel;
- Splitter1: TSplitter;
- Panel2: TPanel;
- Panel3: TPanel;
- Splitter2: TSplitter;
- Panel4: TPanel;
- Label8: TLabel;
- Label9: TLabel;
- Label10: TLabel;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: 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;
- Tree: TTreeView;
- dbEdit1: TEdit;
- DbEdit2: TEdit;
- DbEdit3: TEdit;
- DbEdit4: TEdit;
- ClientDataSet1: TClientDataSet;
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure QuitExecute(Sender: TObject);
- procedure SaveExecute(Sender: TObject);
- procedure AddExecute(Sender: TObject);
- procedure EditExecute(Sender: TObject);
- procedure RefreshExecute(Sender: TObject);
- procedure DeleteExecute(Sender: TObject);
- procedure DBGridEh1CellClick(Column: TColumnEh);
- procedure N5Click(Sender: TObject);
- procedure N6Click(Sender: TObject);
- procedure dbEdit1KeyPress(Sender: TObject; var Key: Char);
- procedure DbEdit2KeyPress(Sender: TObject; var Key: Char);
- procedure FormShow(Sender: TObject);
- procedure TreeClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- procedure loadTree(treeDb:TClientDataSet);//初始化树
- procedure BtnState();//增,改时的按钮变化
- procedure SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
- end;
- var
- sz_bm: Tsz_bm;
- gnode:TtreeNode;//当前节点
- lbjs_sm:string;//类别级数
- lb_dmgz:string;//代码规则
- dsState:string;//增删改的状态
- implementation
- uses dms,mainfrms,u_public;
- {$R *.dfm}
- procedure Tsz_bm.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_bm.SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
- var
- curnode:TTreenode;//已选树节点
- Pnodetxt,nodetxt:string;//Pnodetxt:取出树上级节点节点的代码;nodetxt:要在树节点上增加的值
- n:integer;//取出'-'在已选树节点中的位置
- begin
- dm.sz_bm.edit;
- n:=pos('-',TreeView.Selected.Text)-1;
- Pnodetxt:=copy(TreeView.Selected.Text,1,n);
- nodetxt:=Pnodetxt+dbedit1.Text+'-'+dbedit2.Text;//现在节点等于原来的加上现在的
- //增加数据到节点
- dm.sz_bm.FieldByName('bm_dm').AsString:=Pnodetxt+dbedit1.Text;
- dm.sz_bm.FieldByName('bm_mc').AsString:=dbedit2.Text;
- dm.sz_bm.FieldByName('bm_js').AsInteger:=tree.Selected.Level;
- dm.sz_bm.FieldByName('bm_sm').AsString:=dbEdit4.Text;
- dm.sz_bm.FieldByName('bm_sjbmdm').AsString:=Pnodetxt;
- //给数组赋值
- dm.sz_bm.ApplyUpdates(0);
- application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
- curnode:=TreeView.Items.AddChild(tree.Selected,nodetxt);//增加新的节点
- curnode.ImageIndex:=0;
- curnode.SelectedIndex:=1;
- //-------按钮状态
- end;
- //**************************************************
- procedure Tsz_bm.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('bm_dm').AsString);
- nodeTxt:=nodeId+'-'+trim(fieldbyname('bm_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_bm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- dm.sz_bm.Active:=false;
- dm.xt_lbjs.Active:=false;
- action:=cafree;
- sz_bm:=nil;
- Tree.Free;
- end;
- procedure Tsz_bm.QuitExecute(Sender: TObject);
- begin
- close;
- end;
- procedure Tsz_bm.SaveExecute(Sender: TObject);
- var
- yanzheng:string;//yangzheng查找数据库中是否有重复记录存在
- //Pnodetxt:string;//pnodetxt:父节点值
- i:integer;
- hint:string;
- s1,s2:string;
- Epos:integer;
- begin
- if dbedit1.Text='' then
- begin
- application.MessageBox('部门代码不能为空','提示',mb_ok+mb_iconinformation);
- exit;
- end;
- if dbedit2.Text='' then
- begin
- application.MessageBox('部门名称不能为空','提示',mb_ok+mb_iconinformation);
- 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_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+yanzheng+''''+'';
- dm.sz_bm.Open;
- if dm.sz_bm.FieldByName('bm_dm').AsString<>'' then
- begin
- application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
- BtnState();
- exit;
- end
- else
- begin
- SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
- //refresh1.Click;
- BtnState();//调用按钮的状态参数
- break;//成功则退出循环
- end;
- end;//end if 0
- end//&&&&
- else continue;
- end;//end for
- end//end 结束增加
- else
- begin //修改后的情况
- dm.sz_bm.Edit;
- Tree.Selected.EditText;//修改节点
- Tree.Selected.Text:=Dbedit1.text+'-'+DbEdit2.Text;
- tree.Selected.ImageIndex:=0;
- tree.Selected.SelectedIndex:=1;
- //增加数据到节点
- dm.sz_bm.FieldByName('bm_dm').AsString:=dbedit1.Text;
- dm.sz_bm.FieldByName('bm_mc').AsString:=dbedit2.Text;
- dm.sz_bm.FieldByName('bm_js').AsInteger:=StrToInt(dbedit3.text);
- dm.sz_bm.FieldByName('bm_sm').AsString:=dbEdit4.Text;
- //给数组赋值
- dm.sz_bm.ApplyUpdates(0);
- application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
- BtnState();
- dbedit1.Enabled:=true;
- dbedit1.Color:=clwindow;
- end;
- end;
- procedure Tsz_bm.AddExecute(Sender: TObject);
- begin
- if tree.Selected=nil then
- exit
- else
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+''''+'';
- dm.sz_bm.Open;//增加
- if tree.Selected.level=length(trim(lbjs_sm)) then
- begin
- application.MessageBox('已经到达最底层的节点,不能再增加','提示',mb_ok+mb_iconinformation);
- exit;
- end;
- dbedit1.SetFocus;
- dbedit1.Text:='';dbedit2.Text:='';
- dbedit3.Text:=IntToStr(tree.Selected.level);
- BtnState();//调用按钮的状态参数
- dsState:='add';
- end;
- procedure Tsz_bm.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_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
- dm.sz_bm.Open;
- dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
- Dbedit1.Enabled:=false;
- DBEdit1.Color:=cl3DLight;
- dm.sz_bm.edit;
- end
- else
- begin
- application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
- exit;
- end;
- end;
- procedure Tsz_bm.RefreshExecute(Sender: TObject);
- begin
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm';
- dm.sz_bm.Open;
- end;
- procedure Tsz_bm.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;//删除树形表中数据
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='delete from sz_bm where bm_dm='+''''+s2+''''+'';
- dm.sz_bm.execute;
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
- dm.sz_bm.Open;
- dbedit1.Text:='';dbedit2.text:='';
- dbedit3.Text:='';dbedit4.Text:='';
- Clientdataset1.Close;
- Clientdataset1.CommandText:='select * from sz_bm';
- Clientdataset1.Open;//
- if Clientdataset1.recordcount=0 then
- begin
- dele1.Enabled:=false;
- xiugai.Enabled:=false;
- end;
- //删除表中数据
- end;
- end;
- end;
- procedure Tsz_bm.DBGridEh1CellClick(Column: TColumnEh);
- var
- s1,s2:string;
- i:integer;
- begin//--1单击每一行时,使树形控件也变化
- s1:=DBGridEh1.Columns[1].DisplayText;
- s2:=DBGridEh1.Columns[2].DisplayText;
- dm.sz_bm.Locate('bm_dm','+''''+s1+''''+',[]);
- //过滤要搜索的数据
- dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
- dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
- dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
- dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').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_bm.N5Click(Sender: TObject);
- begin
- tree.Selected.Expand(true);
- end;
- procedure Tsz_bm.N6Click(Sender: TObject);
- begin
- tree.Selected.Collapse(true);
- end;
- procedure Tsz_bm.dbEdit1KeyPress(Sender: TObject; var Key: Char);
- begin
- if key=#13 then
- begin
- key:=#0;
- SelectNext(ActiveControl,true,true);
- end;
- end;
- procedure Tsz_bm.DbEdit2KeyPress(Sender: TObject; var Key: Char);
- begin
- DbEdit1keyPress(nil,key);
- end;
- procedure Tsz_bm.FormShow(Sender: TObject);
- begin
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm';
- dm.sz_bm.Open;
- dm.xt_lbjs.Close;
- dm.xt_lbjs.CommandText:='select lbjs_sm,lb_dmgz from xt_lbjs where lb_dm='+''''+'sz_bm'+''''+'';
- 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_bm);//初始化树
- dm.sz_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm';
- dm.sz_bm.Open;
- end;
- procedure Tsz_bm.TreeClick(Sender: TObject);
- var
- sss:string;
- 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_bm.Close;
- dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
- dm.sz_bm.Open;
- dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
- dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
- dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
- dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').AsString;
- end;
- end;
- end;
- end.