Am_kwgls.pas
上传用户:szruida
上传日期:2007-06-01
资源大小:6518k
文件大小:12k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit Am_kwgls;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, Menus, ActnList, DB, XPBarMenu, Grids, DBGridEh, StdCtrls, Mask,
  6.   DBCtrls,  ComCtrls, ExtCtrls, ToolWin, DBClient, DBCtrlsEh, DBLookupEh;
  7. type
  8.   TAm_kwgl = class(TForm)
  9.     CoolBar1: TCoolBar;
  10.     ToolBar1: TToolBar;
  11.     add1: TToolButton;
  12.     xiugai: TToolButton;
  13.     dele1: TToolButton;
  14.     save1: TToolButton;
  15.     Refresh1: TToolButton;
  16.     Print1: TToolButton;
  17.     Quit1: TToolButton;
  18.     Panel1: TPanel;
  19.     Splitter1: TSplitter;
  20.     Panel2: TPanel;
  21.     Tree: TTreeView;
  22.     Panel3: TPanel;
  23.     Splitter2: TSplitter;
  24.     Panel4: TPanel;
  25.     Panel5: TPanel;
  26.     GroupBox2: TGroupBox;
  27.     DBGridEh1: TDBGridEh;
  28.     XPBarMenu1: TXPBarMenu;
  29.     sz_kw: TDataSource;
  30.     ActionList1: TActionList;
  31.     Add: TAction;
  32.     Edit: TAction;
  33.     Delete: TAction;
  34.     Save: TAction;
  35.     Refresh: TAction;
  36.     Print: TAction;
  37.     Quit: TAction;
  38.     PopupMenu1: TPopupMenu;
  39.     A1: TMenuItem;
  40.     E1: TMenuItem;
  41.     N5: TMenuItem;
  42.     N6: TMenuItem;
  43.     Label1: TLabel;
  44.     DBEdit1: TDBEdit;
  45.     Label2: TLabel;
  46.     Label3: TLabel;
  47.     DBEdit3: TDBEdit;
  48.     Label4: TLabel;
  49.     DBEdit4: TDBEdit;
  50.     Label5: TLabel;
  51.     am_kwzk: TDataSource;
  52.     Label6: TLabel;
  53.     DBMemo1: TDBMemo;
  54.     DeleS: TMenuItem;
  55.     DBEditEh1: TDBEditEh;
  56.     procedure TreeChange(Sender: TObject; Node: TTreeNode);
  57.     procedure AddExecute(Sender: TObject);
  58.     procedure EditExecute(Sender: TObject);
  59.     procedure FormCreate(Sender: TObject);
  60.     procedure TreeClick(Sender: TObject);
  61.     procedure QuitExecute(Sender: TObject);
  62.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  63.     procedure RefreshExecute(Sender: TObject);
  64.     procedure SaveExecute(Sender: TObject);
  65.     procedure DeleteExecute(Sender: TObject);
  66.     procedure DBGridEh1CellClick(Column: TColumnEh);
  67.     procedure N6Click(Sender: TObject);
  68.     procedure N5Click(Sender: TObject);
  69.     procedure DeleSClick(Sender: TObject);
  70.     procedure PrintExecute(Sender: TObject);
  71.   private
  72.     { Private declarations }
  73.   public
  74.     { Public declarations }
  75.     procedure BtnState();
  76.     procedure loadTree(treeDb:TClientDataSet);//初始化树
  77.   end;
  78. var
  79.   Am_kwgl: TAm_kwgl;
  80.   gnode:TtreeNode;//当前节点
  81.   lbjs_sm:string;//类别级数
  82.   lb_dmgz:string;//代码规则
  83.   dsState:string;//确定是增,删,改
  84. implementation
  85. uses dms,u_public, Am_Kwgl_Ps, am_kwzk_ps, am_kwzkps;
  86. {$R *.dfm}
  87. procedure TAm_kwgl.BtnState();
  88. begin
  89.   if add1.Enabled=true or xiugai.Enabled=true then
  90.   begin
  91.     add1.Enabled:=false;A1.Enabled:=false;
  92.     xiugai.Enabled:=false;e1.Enabled:=false;
  93.     dele1.Enabled:=false;DeleS.Enabled:=false;
  94.     save1.Enabled:=true;
  95.     refresh1.Enabled:=true;
  96.   end
  97.   else
  98.   begin
  99.     add1.Enabled:=true;A1.Enabled:=true;
  100.     xiugai.Enabled:=true;E1.Enabled:=true;
  101.     dele1.Enabled:=true;DeleS.Enabled:=true;
  102.     save1.Enabled:=false;
  103.     refresh1.Enabled:=true;
  104.   end;
  105. end;
  106. //********************************************************************
  107. procedure TAm_kwgl.loadTree(treeDb:TClientDataSet);//初始化树
  108. var
  109.   nodeId,nodeTxt:string;
  110.   SCodeFormat,SFirstNodeTxt:string;
  111.   level:integer;
  112.   mynode:array[0..6] of Ttreenode;
  113. begin
  114.  //初始化变量
  115.    SCodeFormat:=lbjs_sm ;   //库位代码结构
  116.    SFirstNodeTxt:= '库位';  //首节点显示的文字
  117.    Screen.Cursor:=crHourGlass;
  118.    Tree.enabled:=true;
  119.    tree.Items.Clear;
  120.    level:=0;
  121.  //设置根节点
  122.  mynode[level]:=tree.Items.Add(tree.TopItem,'库位');
  123.  mynode[level].ImageIndex:=0;
  124.  mynode[level].SelectedIndex:=1;
  125.  //遍历数据表,利用编码字段记录排序规律,依次添加树节点
  126.  with treeDB do
  127.  begin
  128.     try
  129.       if not active then open;
  130.       first;
  131.       while not treedb.Eof do
  132.       begin
  133.         nodeId:=trim(fieldbyname('kw_dm').AsString);
  134.         nodeTxt:=nodeId+'-'+trim(fieldbyname('kw_mc').AsString);
  135.         level:=GetNodeLevel(SCodeFormat,nodeId);//获得节点层树
  136.         //再这里返回代码的层次数
  137.         if level>0 then
  138.         begin
  139.            //增加下一节点时,用添加子节点的方法可以轻松实现节点的层次关系
  140.            //注意,这里的父节点是用当前节点的上一级节点mynode[level-1]
  141.            mynode[level]:=tree.Items.AddChild(mynode[level-1],nodeTxt);
  142.            mynode[level].ImageIndex:=0;
  143.            mynode[level].SelectedIndex:=1;
  144.         end;//end if
  145.       next;//下一条纪录
  146.       end;//end while
  147.     finally;
  148.       close;
  149.     end;
  150.     mynode[0].Expand(false);
  151.     screen.Cursor:=crdefault;
  152.  end;//end with
  153. end;
  154. procedure TAm_kwgl.TreeChange(Sender: TObject; Node: TTreeNode);
  155. begin
  156.  {if dsState='add' then
  157.      if application.MessageBox('请输入数据','提示',mb_okcancel+mb_iconinformation)=idOk then
  158.        //dbedit1.SetFocus
  159.        add.Execute
  160.      else
  161.      begin
  162.        BtnState();//确定按钮变回来
  163.      end; }
  164.   dbediteh1.Text:='';
  165. end;
  166. procedure TAm_kwgl.AddExecute(Sender: TObject);
  167. var s1,s2:string;
  168.    Epos:integer;
  169. begin
  170.  if tree.Selected.HasChildren then
  171.   begin
  172.     APPLICATION.MessageBox('必须在最底层增加','提示',MB_ok+mb_iconinformation);
  173.     exit;
  174.   end
  175.   else
  176.   s1:=Tree.Selected.Text;
  177.   Epos:=pos('-',s1)-1;//取得'-'在节点名中的位置
  178.   s2:=copy(s1,0,Epos);//
  179.   dm.sz_kw.Close;
  180.   dm.sz_kw.CommandText:='select kw_mc,kw_dm from sz_kw where kw_dm='+''''+s2+''''+'';
  181.   dm.sz_kw.Open;
  182.   DbEditEh1.Text:=dm.sz_kw.fieldbyname('kw_mc').AsString;
  183.   //-------------------------------------
  184.   dm.Am_kwzk.Append;
  185.   dbedit3.SetFocus;
  186.   dbedit1.Text:=formatdatetime('yyyy-mm-dd hh:mm:ss',now());
  187.   BtnState();//调用按钮的状态参数
  188.   dsState:='add';
  189. end;
  190. procedure TAm_kwgl.EditExecute(Sender: TObject);
  191. var s1,s2:string;
  192.     Epos:integer;
  193. begin
  194.    if tree.selected=nil then
  195.       exit
  196.     else
  197.    if tree.Items.Count<>0 then
  198.    if Tree.Items[0].HasChildren<>false then
  199.    begin
  200.        BtnState();//调用按钮的状态参数
  201.        s1:=Tree.Selected.Text;
  202.        Epos:=pos('-',s1)-1;//取得'-'在节点名中的位置
  203.        s2:=copy(s1,0,Epos);//
  204.        dm.sz_kw.Close;
  205.        dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+s2+''''+'';
  206.        dm.sz_kw.Open;
  207.        DbEditEh1.Text:=dm.sz_kw.fieldbyname('kw_mc').AsString;
  208.        dm.am_kwzk.edit;
  209.    end
  210.    else
  211.    begin
  212.       application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
  213.       exit;
  214.    end;
  215. end;
  216. procedure TAm_kwgl.FormCreate(Sender: TObject);
  217. begin
  218.    dm.sz_kw.Close;
  219.    dm.sz_kw.CommandText:='select * from sz_kw';
  220.    dm.sz_kw.Open;
  221.    //-----------------------------------------------
  222.    dm.xt_lbjs.close;
  223.    dm.xt_lbjs.CommandText:='select * from xt_lbjs where lb_dm='+''''+'sz_kw'+''''+'';
  224.    dm.xt_lbjs.Open;
  225.    lbjs_sm:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
  226.    loadTree(dm.sz_kw);//初始化树
  227.    dm.sz_kw.Close;
  228.    dm.sz_kw.CommandText:='select * from sz_kw';
  229.    dm.sz_kw.Open;
  230.    {dm.Am_kwzk.Close;
  231.    dm.Am_kwzk.CommandText:='select * from am_kwzk';
  232.    dm.Am_kwzk.Open; }
  233.    Print1.Enabled:=true;
  234. end;
  235. procedure TAm_kwgl.TreeClick(Sender: TObject);
  236. var
  237.    s1,s2,s3:string;
  238.    i,Epos:integer;
  239. begin
  240.   if tree.selected=nil then
  241.      exit
  242.   else
  243.  if Tree.Items[0].HasChildren<>false then
  244.   begin   //选中和不选中的图片显示
  245.     for i:=0 to Tree.items.count-1 do
  246.     begin
  247.       Tree.Items[i].ImageIndex:=0;
  248.       Tree.Items[i].SelectedIndex:=1;
  249.         s1:=Tree.Selected.Text;
  250.         Epos:=pos('-',s1);
  251.         s2:=copy(s1,0,Epos-1);
  252.         s3:=copy(s1,Epos+1,length(s1));
  253.         dm.Am_kwzk.Close;
  254.         dm.Am_kwzk.CommandText:='select * from Am_kwzk where kw_dm='+''''+s2+''''+'';
  255.         dm.Am_kwzk.Open;
  256.         if tree.Selected.HasChildren then
  257.          begin
  258.            xiugai.Enabled:=false;E1.Enabled:=false;
  259.            dele1.Enabled:=false;DeleS.Enabled:=false;
  260.          end
  261.         else
  262.          begin
  263.            if trim(dm.Am_kwzk.FieldByName('kw_dm').AsString)='' then
  264.             begin
  265.               DbEditEh1.Text:='';
  266.               xiugai.Enabled:=false;E1.Enabled:=false;
  267.               dele1.Enabled:=false;DeleS.Enabled:=false;
  268.             end
  269.            else
  270.             begin
  271.              DbEditEh1.Text:=s3;
  272.              xiugai.Enabled:=true;E1.Enabled:=true;
  273.              dele1.Enabled:=true;DeleS.Enabled:=true;
  274.             end;
  275.          end;
  276.       end;
  277.  end;
  278. end;
  279. procedure TAm_kwgl.QuitExecute(Sender: TObject);
  280. begin
  281.   close;
  282. end;
  283. procedure TAm_kwgl.FormClose(Sender: TObject; var Action: TCloseAction);
  284. begin
  285.  dm.Am_kwzk.active:=false;
  286.  dm.sz_kw.Active:=false;
  287.  tree.Free;
  288.  action:=cafree;
  289.  am_kwgl:=nil;
  290. end;
  291. procedure TAm_kwgl.RefreshExecute(Sender: TObject);
  292. begin
  293.   dm.Am_kwzk.Close;
  294.   dm.Am_kwzk.CommandText:='select * from am_kwzk';
  295.   dm.Am_kwzk.Open;
  296.   if dm.Am_kwzk.RecordCount=0 then
  297.    begin
  298.      add1.Enabled:=true;A1.Enabled:=true;
  299.      xiugai.Enabled:=false;e1.Enabled:=false;
  300.      dele1.Enabled:=false;DeleS.Enabled:=false;
  301.      save1.Enabled:=false;
  302.    end
  303.   else
  304.    begin
  305.      add1.Enabled:=true;A1.Enabled:=true;
  306.      xiugai.Enabled:=true;e1.Enabled:=true;
  307.      dele1.Enabled:=true;DeleS.Enabled:=true;
  308.      save1.Enabled:=false;
  309.    end;
  310. end;
  311. procedure TAm_kwgl.SaveExecute(Sender: TObject);
  312. var n,errorI:integer;
  313.     s_kwmc:string;
  314. begin
  315.   dm.am_kwzk.edit;
  316.   n:=pos('-',tree.Selected.Text)-1;
  317.   s_kwmc:=copy(tree.Selected.Text,0,n);
  318.   dm.sz_kw.Close;
  319.   dm.sz_kw.CommandText:='select kw_dm from sz_kw where kw_dm='+''''+s_kwmc+''''+'';
  320.   dm.sz_kw.Open;
  321.   dm.Am_kwzk.FieldByName('kw_dm').AsString:=dm.sz_kw.fieldbyname('kw_dm').AsString;
  322.   dm.Am_kwzk.ApplyUpdates(0);
  323.   errorI:=dm.Am_kwzk.ApplyUpdates(0);
  324.   if errorI=0 then
  325.   begin
  326.      application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  327.      BtnState();
  328.      exit;
  329.   end
  330.   else
  331.    begin
  332.      application.MessageBox('数据保存失败','提示',mb_ok+mb_iconinformation);
  333.      BtnState();
  334.      exit;
  335.    end;
  336. end;
  337. procedure TAm_kwgl.DeleteExecute(Sender: TObject);
  338. var
  339.    epos:integer;
  340.    s1,s2,s:string;
  341. begin
  342.   if tree.Selected=nil then
  343.      exit
  344.   else
  345.   if tree.Items[0].HasChildren then
  346.    if tree.Selected.HasChildren=true then
  347.     begin
  348.      application.messagebox('该部门包含下级,不能删除','提示',MB_OKCANCEL+mb_iconinformation);
  349.     end
  350.     else
  351.     begin
  352.        s1:=Tree.Selected.Text;
  353.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  354.        s2:=copy(s1,0,Epos-1);//
  355.        dm.Am_kwzk.Close;
  356.        dm.Am_kwzk.CommandText:='select * from Am_kwzk where kw_dm='+''''+s2+''''+'';
  357.        dm.Am_kwzk.open;
  358.        if dm.Am_kwzk.FieldByName('kw_dm').AsString='' then
  359.           application.MessageBox('当前表中没有数据'+#13#10+'不能删除','提示',mb_ok+mb_iconinformation)
  360.        else
  361.         if application.messagebox('您确实要删除当前记录吗?','提示',MB_OKCANCEL+mb_iconquestion )=IDOK then
  362.       begin
  363.        //-----------------
  364.        s:=dm.am_kwzk.fieldbyname('kwzk_xh').AsString;
  365.        dm.Am_kwzk.Close;
  366.        dm.Am_kwzk.CommandText:='delete  from Am_kwzk where kwzk_xh='+''''+s+''''+'';
  367.        dm.Am_kwzk.execute;
  368.        dm.Am_kwzk.Close;
  369.        dm.Am_kwzk.CommandText:='select * from Am_kwzk where kw_dm='+''''+s2+''''+'';
  370.        dm.Am_kwzk.Open;
  371.        //删除表中数据
  372.       end;
  373.    end;
  374. end;
  375. //******************************************************************************
  376. procedure TAm_kwgl.DBGridEh1CellClick(Column: TColumnEh);
  377. var
  378.   s1:string;
  379.   i:integer;
  380. begin//--1单击每一行时,使树形控件也变化
  381.   s1:=DBGridEh1.Columns[0].DisplayText;//易犯错误是表格的Columns弄错0,1,2
  382.   dm.sz_kw.Close;
  383.   dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+s1+''''+'';
  384.   dm.sz_kw.Open;
  385.   Dbediteh1.Text:=dm.sz_kw.fieldbyname('kw_mc').AsString;
  386.  If Tree.Items[0].HasChildren<>false then
  387.   begin//end if
  388.     for i:=0 to Tree.items.count-1 do
  389.       if Tree.Items[i].Text=s1+'-'+DbEditEh1.text then
  390.      begin
  391.          Tree.Items[i].Selected:=true;
  392.          break;
  393.      end;//end for
  394.   end;//end if
  395. end;//--1
  396. procedure TAm_kwgl.N6Click(Sender: TObject);
  397. begin
  398.  tree.Selected.Collapse(true);
  399. end;
  400. procedure TAm_kwgl.N5Click(Sender: TObject);
  401. begin
  402.   tree.Selected.Expand(true);
  403. end;
  404. procedure TAm_kwgl.DeleSClick(Sender: TObject);
  405. begin
  406.  dele1.Click;
  407. end;   
  408. procedure TAm_kwgl.PrintExecute(Sender: TObject);
  409. begin
  410.  am_kwzkp:=Tam_kwzkp.create(self);
  411.  am_kwzkp.quickrep1.preview;
  412. end;
  413. end.