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

Delphi控件源码

开发平台:

Delphi

  1. unit sz_bms;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, Menus, ActnList, DB, XPBarMenu, Grids, DBGrids, StdCtrls, Mask,
  6.   DBCtrls, ComCtrls, ExtCtrls, ToolWin, DBGridEh, DBClient, DBCtrlsEh;
  7. type
  8.   Tsz_bm = class(TForm)
  9.     CoolBar1: TCoolBar;
  10.     ToolBar1: TToolBar;
  11.     add1: TToolButton;
  12.     xiugai: TToolButton;
  13.     dele1: TToolButton;
  14.     save1: TToolButton;
  15.     refresh1: TToolButton;
  16.     ToolButton3: TToolButton;
  17.     Panel1: TPanel;
  18.     Splitter1: TSplitter;
  19.     Panel2: TPanel;
  20.     Panel3: TPanel;
  21.     Splitter2: TSplitter;
  22.     Panel4: TPanel;
  23.     Label8: TLabel;
  24.     Label9: TLabel;
  25.     Label10: TLabel;
  26.     Label1: TLabel;
  27.     Label2: TLabel;
  28.     Label3: TLabel;
  29.     Label4: TLabel;
  30.     Edit1: TEdit;
  31.     Panel5: TPanel;
  32.     GroupBox2: TGroupBox;
  33.     DBGridEh1: TDBGridEh;
  34.     XPBarMenu1: TXPBarMenu;
  35.     DataSource1: TDataSource;
  36.     ActionList1: TActionList;
  37.     Add: TAction;
  38.     Edit: TAction;
  39.     Delete: TAction;
  40.     Save: TAction;
  41.     Refresh: TAction;
  42.     Print: TAction;
  43.     Quit: TAction;
  44.     PopupMenu1: TPopupMenu;
  45.     A1: TMenuItem;
  46.     D1: TMenuItem;
  47.     E1: TMenuItem;
  48.     N5: TMenuItem;
  49.     N6: TMenuItem;
  50.     Tree: TTreeView;
  51.     dbEdit1: TEdit;
  52.     DbEdit2: TEdit;
  53.     DbEdit3: TEdit;
  54.     DbEdit4: TEdit;
  55.     ClientDataSet1: TClientDataSet;
  56.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  57.     procedure QuitExecute(Sender: TObject);
  58.     procedure SaveExecute(Sender: TObject);
  59.     procedure AddExecute(Sender: TObject);
  60.     procedure EditExecute(Sender: TObject);
  61.     procedure RefreshExecute(Sender: TObject);
  62.     procedure DeleteExecute(Sender: TObject);
  63.     procedure DBGridEh1CellClick(Column: TColumnEh);
  64.     procedure N5Click(Sender: TObject);
  65.     procedure N6Click(Sender: TObject);
  66.     procedure dbEdit1KeyPress(Sender: TObject; var Key: Char);
  67.     procedure DbEdit2KeyPress(Sender: TObject; var Key: Char);
  68.     procedure FormShow(Sender: TObject);
  69.     procedure TreeClick(Sender: TObject);
  70.   private
  71.     { Private declarations }
  72.   public
  73.     { Public declarations }
  74.     procedure  loadTree(treeDb:TClientDataSet);//初始化树
  75.     procedure BtnState();//增,改时的按钮变化
  76.     procedure SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
  77.   end;
  78. var
  79.   sz_bm: Tsz_bm;
  80.   gnode:TtreeNode;//当前节点
  81.   lbjs_sm:string;//类别级数
  82.   lb_dmgz:string;//代码规则
  83.   dsState:string;//增删改的状态
  84. implementation
  85. uses dms,mainfrms,u_public;
  86. {$R *.dfm}
  87. procedure Tsz_bm.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;D1.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;D1.Enabled:=true;
  102.     save1.Enabled:=false;
  103.     refresh1.Enabled:=true;
  104.   end;
  105. end;
  106. //***********************************************
  107. procedure Tsz_bm.SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
  108. var
  109.   curnode:TTreenode;//已选树节点
  110.   Pnodetxt,nodetxt:string;//Pnodetxt:取出树上级节点节点的代码;nodetxt:要在树节点上增加的值
  111.   n:integer;//取出'-'在已选树节点中的位置
  112. begin
  113.    dm.sz_bm.edit;
  114.    n:=pos('-',TreeView.Selected.Text)-1;
  115.    Pnodetxt:=copy(TreeView.Selected.Text,1,n);
  116.    nodetxt:=Pnodetxt+dbedit1.Text+'-'+dbedit2.Text;//现在节点等于原来的加上现在的
  117.    //增加数据到节点
  118.    dm.sz_bm.FieldByName('bm_dm').AsString:=Pnodetxt+dbedit1.Text;
  119.    dm.sz_bm.FieldByName('bm_mc').AsString:=dbedit2.Text;
  120.    dm.sz_bm.FieldByName('bm_js').AsInteger:=tree.Selected.Level;
  121.    dm.sz_bm.FieldByName('bm_sm').AsString:=dbEdit4.Text;
  122.    dm.sz_bm.FieldByName('bm_sjbmdm').AsString:=Pnodetxt;
  123.    //给数组赋值
  124.    dm.sz_bm.ApplyUpdates(0);
  125.    application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  126.    curnode:=TreeView.Items.AddChild(tree.Selected,nodetxt);//增加新的节点
  127.    curnode.ImageIndex:=0;
  128.    curnode.SelectedIndex:=1;
  129.    //-------按钮状态
  130. end;
  131. //**************************************************
  132. procedure Tsz_bm.loadTree(treeDb:TClientDataSet);//初始化树
  133. var
  134.   nodeId,nodeTxt:string;
  135.   SCodeFormat,SFirstNodeTxt:string;
  136.   level:integer;
  137.   mynode:array[0..6] of Ttreenode;
  138. begin
  139.  //初始化变量
  140.    SCodeFormat:=lbjs_sm ;   //库位代码结构
  141.    SFirstNodeTxt:= '部门';  //首节点显示的文字
  142.    Screen.Cursor:=crHourGlass;
  143.    Tree.enabled:=true;
  144.    tree.Items.Clear;
  145.    level:=0;
  146.  //设置根节点
  147.  mynode[level]:=tree.Items.Add(tree.TopItem,'库位');
  148.  mynode[level].ImageIndex:=0;
  149.  mynode[level].SelectedIndex:=1;
  150.  //遍历数据表,利用编码字段记录排序规律,依次添加树节点
  151.  with treeDB do
  152.  begin
  153.     try
  154.       if not active then open;
  155.       first;
  156.       while not treedb.Eof do
  157.       begin
  158.         nodeId:=trim(fieldbyname('bm_dm').AsString);
  159.         nodeTxt:=nodeId+'-'+trim(fieldbyname('bm_mc').AsString);
  160.         level:=GetNodeLevel(SCodeFormat,nodeId);//获得节点层树
  161.         //再这里返回代码的层次数
  162.         if level>0 then
  163.         begin
  164.            //增加下一节点时,用添加子节点的方法可以轻松实现节点的层次关系
  165.            //注意,这里的父节点是用当前节点的上一级节点mynode[level-1]
  166.            mynode[level]:=tree.Items.AddChild(mynode[level-1],nodeTxt);
  167.            mynode[level].ImageIndex:=0;
  168.            mynode[level].SelectedIndex:=1;
  169.         end;//end if
  170.       next;//下一条纪录
  171.       end;//end while
  172.     finally;
  173.       close;
  174.     end;
  175.     mynode[0].Expand(false);
  176.     screen.Cursor:=crdefault;
  177.  end;//end with
  178. end;
  179. //*********************************************************************
  180. procedure Tsz_bm.FormClose(Sender: TObject; var Action: TCloseAction);
  181. begin
  182.   dm.sz_bm.Active:=false;
  183.   dm.xt_lbjs.Active:=false;
  184.   action:=cafree;
  185.   sz_bm:=nil;
  186.   Tree.Free;
  187. end;
  188. procedure Tsz_bm.QuitExecute(Sender: TObject);
  189. begin
  190.  close;
  191. end;
  192. procedure Tsz_bm.SaveExecute(Sender: TObject);
  193. var
  194.   yanzheng:string;//yangzheng查找数据库中是否有重复记录存在
  195.   //Pnodetxt:string;//pnodetxt:父节点值
  196.   i:integer;
  197.   hint:string;
  198.   s1,s2:string;
  199.   Epos:integer;
  200. begin
  201.   if dbedit1.Text='' then
  202.    begin
  203.        application.MessageBox('部门代码不能为空','提示',mb_ok+mb_iconinformation);
  204.        exit;
  205.    end;
  206.   if dbedit2.Text='' then
  207.    begin
  208.        application.MessageBox('部门名称不能为空','提示',mb_ok+mb_iconinformation);
  209.        exit;
  210.    end;
  211.   //-----------------------------------
  212.  //判断是增加还是修改*****************************************************
  213.   if dbedit1.Enabled=true then
  214.    begin
  215.      if tree.Selected.Level=length(lbjs_sm) then
  216.       begin
  217.         application.MessageBox('已经到达最底层节点'#13#10'不能增加','提示',mb_ok+mb_iconinformation);
  218.         exit;
  219.       end  else
  220.       for i:=0 to tree.Selected.Level do
  221.        begin
  222.          if tree.Selected.Level=i then
  223.           begin//&&&&
  224.                if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,i+1,1)) then
  225.                begin
  226.                  hint:='请输入'+copy(lbjs_sm,i+1,1)+'位代码';
  227.                  application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  228.                  exit;
  229.                end
  230.              else//
  231.                begin
  232.                  s1:=Tree.Selected.Text;
  233.                  Epos:=pos('-',s1);//取得'-'在节点名中的位置
  234.                  s2:=copy(s1,0,Epos-1);//
  235.                  yanzheng:=s2+dbedit1.Text;
  236.                  dm.sz_bm.Close;
  237.                  dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+yanzheng+''''+'';
  238.                  dm.sz_bm.Open;
  239.                 if dm.sz_bm.FieldByName('bm_dm').AsString<>'' then
  240.                 begin
  241.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  242.                    BtnState();
  243.                    exit;
  244.                 end
  245.                 else
  246.                 begin
  247.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  248.                    //refresh1.Click;
  249.                    BtnState();//调用按钮的状态参数
  250.                    break;//成功则退出循环
  251.                 end;
  252.              end;//end if 0
  253.           end//&&&&
  254.           else continue;
  255.        end;//end for
  256.    end//end 结束增加
  257.    else
  258.     begin //修改后的情况
  259.       dm.sz_bm.Edit;
  260.       Tree.Selected.EditText;//修改节点
  261.       Tree.Selected.Text:=Dbedit1.text+'-'+DbEdit2.Text;
  262.       tree.Selected.ImageIndex:=0;
  263.       tree.Selected.SelectedIndex:=1;
  264.       //增加数据到节点
  265.       dm.sz_bm.FieldByName('bm_dm').AsString:=dbedit1.Text;
  266.       dm.sz_bm.FieldByName('bm_mc').AsString:=dbedit2.Text;
  267.       dm.sz_bm.FieldByName('bm_js').AsInteger:=StrToInt(dbedit3.text);
  268.       dm.sz_bm.FieldByName('bm_sm').AsString:=dbEdit4.Text;
  269.       //给数组赋值
  270.        dm.sz_bm.ApplyUpdates(0);
  271.        application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  272.        BtnState();
  273.        dbedit1.Enabled:=true;
  274.        dbedit1.Color:=clwindow;
  275.     end;
  276. end;
  277. procedure Tsz_bm.AddExecute(Sender: TObject);
  278. begin
  279.  if tree.Selected=nil then
  280.       exit
  281.   else
  282.   dm.sz_bm.Close;
  283.   dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+''''+'';
  284.   dm.sz_bm.Open;//增加
  285.   if tree.Selected.level=length(trim(lbjs_sm)) then
  286.   begin
  287.      application.MessageBox('已经到达最底层的节点,不能再增加','提示',mb_ok+mb_iconinformation);
  288.      exit;
  289.   end;
  290.   dbedit1.SetFocus;
  291.   dbedit1.Text:='';dbedit2.Text:='';
  292.   dbedit3.Text:=IntToStr(tree.Selected.level);
  293.   BtnState();//调用按钮的状态参数
  294.   dsState:='add';
  295. end;
  296. procedure Tsz_bm.EditExecute(Sender: TObject);
  297. var s1,s2:string;
  298.     Epos:integer;
  299. begin
  300.    if tree.selected=nil then
  301.       exit
  302.     else  
  303.    if tree.Items.Count<>0 then
  304.    if Tree.Items[0].HasChildren<>false then
  305.    begin
  306.        BtnState();//调用按钮的状态参数
  307.        s1:=Tree.Selected.Text;
  308.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  309.        s2:=copy(s1,0,Epos-1);//
  310.        dm.sz_bm.Close;
  311.        dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  312.        dm.sz_bm.Open;
  313.        dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  314.        Dbedit1.Enabled:=false;
  315.        DBEdit1.Color:=cl3DLight;
  316.        dm.sz_bm.edit;
  317.    end
  318.    else
  319.    begin
  320.       application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
  321.       exit;
  322.    end;
  323. end;
  324. procedure Tsz_bm.RefreshExecute(Sender: TObject);
  325. begin
  326.   dm.sz_bm.Close;
  327.   dm.sz_bm.CommandText:='select * from sz_bm';
  328.   dm.sz_bm.Open;
  329. end;
  330. procedure Tsz_bm.DeleteExecute(Sender: TObject);
  331. var
  332.    epos:integer;
  333.    s1,s2:string;
  334. begin
  335.   if tree.Selected=nil then
  336.      exit
  337.   else
  338.   if tree.Items[0].HasChildren then
  339.    if tree.Selected.HasChildren=true then
  340.     begin
  341.      application.messagebox('该部门包含下级,不能删除','提示',MB_OKCANCEL+mb_iconinformation );
  342.     end
  343.     else
  344.     begin
  345.      if application.messagebox('您确定要删除当前纪录吗?','警告',MB_OKCANCEL+mb_iconquestion )=IDOK then
  346.      begin
  347.        s1:=Tree.Selected.Text;
  348.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  349.        s2:=copy(s1,0,Epos-1);//
  350.        tree.Selected.Delete;//删除树形表中数据
  351.        dm.sz_bm.Close;
  352.        dm.sz_bm.CommandText:='delete  from sz_bm where bm_dm='+''''+s2+''''+'';
  353.        dm.sz_bm.execute;
  354.        dm.sz_bm.Close;
  355.        dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  356.        dm.sz_bm.Open;
  357.        dbedit1.Text:='';dbedit2.text:='';
  358.        dbedit3.Text:='';dbedit4.Text:='';
  359.        Clientdataset1.Close;
  360.        Clientdataset1.CommandText:='select * from sz_bm';
  361.        Clientdataset1.Open;//
  362.        if Clientdataset1.recordcount=0 then
  363.          begin
  364.            dele1.Enabled:=false;
  365.            xiugai.Enabled:=false;
  366.          end;
  367.        //删除表中数据
  368.      end;
  369.    end;
  370. end;
  371. procedure Tsz_bm.DBGridEh1CellClick(Column: TColumnEh);
  372. var
  373.   s1,s2:string;
  374.   i:integer;
  375. begin//--1单击每一行时,使树形控件也变化
  376.   s1:=DBGridEh1.Columns[1].DisplayText;
  377.   s2:=DBGridEh1.Columns[2].DisplayText;
  378.   dm.sz_bm.Locate('bm_dm','+''''+s1+''''+',[]);
  379.   //过滤要搜索的数据
  380.   dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  381.   dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
  382.   dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
  383.   dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').AsString;
  384.   if Tree.Items[0].HasChildren<>false then
  385.   begin//end if
  386.     for i:=0 to Tree.items.count-1 do
  387.       if Tree.Items[i].Text=s1+'-'+s2 then
  388.      begin
  389.          Tree.Items[i].Selected:=true;
  390.          break;
  391.      end;//end for
  392.   end;//end if
  393. end;//--1
  394. procedure Tsz_bm.N5Click(Sender: TObject);
  395. begin
  396.   tree.Selected.Expand(true);
  397. end;
  398. procedure Tsz_bm.N6Click(Sender: TObject);
  399. begin
  400.   tree.Selected.Collapse(true);
  401. end;
  402. procedure Tsz_bm.dbEdit1KeyPress(Sender: TObject; var Key: Char);
  403. begin
  404.  if key=#13 then
  405.   begin
  406.      key:=#0;
  407.      SelectNext(ActiveControl,true,true);
  408.   end;   
  409. end;
  410. procedure Tsz_bm.DbEdit2KeyPress(Sender: TObject; var Key: Char);
  411. begin
  412.   DbEdit1keyPress(nil,key);
  413. end;
  414. procedure Tsz_bm.FormShow(Sender: TObject);
  415. begin
  416.   dm.sz_bm.Close;
  417.   dm.sz_bm.CommandText:='select * from sz_bm';
  418.   dm.sz_bm.Open;
  419.   dm.xt_lbjs.Close;
  420.   dm.xt_lbjs.CommandText:='select lbjs_sm,lb_dmgz from xt_lbjs where lb_dm='+''''+'sz_bm'+''''+'';
  421.   dm.xt_lbjs.Open;
  422.   lbjs_sm:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
  423.   lb_dmgz:=dm.xt_lbjs.fieldbyname('lb_dmgz').AsString;
  424.   edit1.Text:=lb_dmgz;
  425.   //-----------------------------------------------
  426.    loadTree(dm.sz_bm);//初始化树
  427.    dm.sz_bm.Close;
  428.    dm.sz_bm.CommandText:='select * from sz_bm';
  429.    dm.sz_bm.Open;
  430. end;
  431. procedure Tsz_bm.TreeClick(Sender: TObject);
  432. var
  433.   sss:string;
  434.    s1,s2:string;
  435.    i,Epos:integer;
  436. begin
  437.   if tree.selected=nil then
  438.      exit
  439.   else
  440.  if Tree.Items[0].HasChildren<>false then
  441.  begin   //选中和不选中的图片显示
  442.     for i:=0 to Tree.items.count-1 do
  443.     begin
  444.       Tree.Items[i].ImageIndex:=0;
  445.       Tree.Items[i].SelectedIndex:=1;
  446.       s1:=Tree.Selected.Text;
  447.       Epos:=pos('-',s1);
  448.       s2:=copy(s1,0,Epos-1);
  449.       dm.sz_bm.Close;
  450.       dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  451.       dm.sz_bm.Open;
  452.       dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  453.       dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
  454.       dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
  455.       dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').AsString;
  456.     end;
  457.  end;
  458. end;
  459. end.