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.   dbedit3.Text:=IntToStr(tree.Selected.level);
  292.   BtnState();//调用按钮的状态参数
  293.   dsState:='add';
  294. end;
  295. procedure Tsz_bm.EditExecute(Sender: TObject);
  296. var s1,s2:string;
  297.     Epos:integer;
  298. begin
  299.    if tree.selected=nil then
  300.       exit
  301.     else  
  302.    if tree.Items.Count<>0 then
  303.    if Tree.Items[0].HasChildren<>false then
  304.    begin
  305.        BtnState();//调用按钮的状态参数
  306.        s1:=Tree.Selected.Text;
  307.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  308.        s2:=copy(s1,0,Epos-1);//
  309.        dm.sz_bm.Close;
  310.        dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  311.        dm.sz_bm.Open;
  312.        dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  313.        Dbedit1.Enabled:=false;
  314.        DBEdit1.Color:=cl3DLight;
  315.        dm.sz_bm.edit;
  316.    end
  317.    else
  318.    begin
  319.       application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
  320.       exit;
  321.    end;
  322. end;
  323. procedure Tsz_bm.RefreshExecute(Sender: TObject);
  324. begin
  325.   dm.sz_bm.Close;
  326.   dm.sz_bm.CommandText:='select * from sz_bm';
  327.   dm.sz_bm.Open;
  328. end;
  329. procedure Tsz_bm.DeleteExecute(Sender: TObject);
  330. var
  331.    epos:integer;
  332.    s1,s2:string;
  333. begin
  334.   if tree.Selected=nil then
  335.      exit
  336.   else
  337.   if tree.Items[0].HasChildren then
  338.    if tree.Selected.HasChildren=true then
  339.     begin
  340.      application.messagebox('该部门包含下级,不能删除','提示',MB_OKCANCEL+mb_iconinformation );
  341.     end
  342.     else
  343.     begin
  344.      if application.messagebox('您确定要删除当前纪录吗?','警告',MB_OKCANCEL+mb_iconquestion )=IDOK then
  345.      begin
  346.        s1:=Tree.Selected.Text;
  347.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  348.        s2:=copy(s1,0,Epos-1);//
  349.        tree.Selected.Delete;//删除树形表中数据
  350.        dm.sz_bm.Close;
  351.        dm.sz_bm.CommandText:='delete  from sz_bm where bm_dm='+''''+s2+''''+'';
  352.        dm.sz_bm.execute;
  353.        dm.sz_bm.Close;
  354.        dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  355.        dm.sz_bm.Open;
  356.        dbedit1.Text:='';dbedit2.text:='';
  357.        dbedit3.Text:='';dbedit4.Text:='';
  358.        Clientdataset1.Close;
  359.        Clientdataset1.CommandText:='select * from sz_bm';
  360.        Clientdataset1.Open;//
  361.        if Clientdataset1.recordcount=0 then
  362.          begin
  363.            dele1.Enabled:=false;
  364.            xiugai.Enabled:=false;
  365.          end;
  366.        //删除表中数据
  367.      end;
  368.    end;
  369. end;
  370. procedure Tsz_bm.DBGridEh1CellClick(Column: TColumnEh);
  371. var
  372.   s1,s2:string;
  373.   i:integer;
  374. begin//--1单击每一行时,使树形控件也变化
  375.   s1:=DBGridEh1.Columns[1].DisplayText;
  376.   s2:=DBGridEh1.Columns[2].DisplayText;
  377.   dm.sz_bm.Locate('bm_dm','+''''+s1+''''+',[]);
  378.   //过滤要搜索的数据
  379.   dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  380.   dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
  381.   dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
  382.   dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').AsString;
  383.   if Tree.Items[0].HasChildren<>false then
  384.   begin//end if
  385.     for i:=0 to Tree.items.count-1 do
  386.       if Tree.Items[i].Text=s1+'-'+s2 then
  387.      begin
  388.          Tree.Items[i].Selected:=true;
  389.          break;
  390.      end;//end for
  391.   end;//end if
  392. end;//--1
  393. procedure Tsz_bm.N5Click(Sender: TObject);
  394. begin
  395.   tree.Selected.Expand(true);
  396. end;
  397. procedure Tsz_bm.N6Click(Sender: TObject);
  398. begin
  399.   tree.Selected.Collapse(true);
  400. end;
  401. procedure Tsz_bm.dbEdit1KeyPress(Sender: TObject; var Key: Char);
  402. begin
  403.  if key=#13 then
  404.   begin
  405.      key:=#0;
  406.      SelectNext(ActiveControl,true,true);
  407.   end;   
  408. end;
  409. procedure Tsz_bm.DbEdit2KeyPress(Sender: TObject; var Key: Char);
  410. begin
  411.   DbEdit1keyPress(nil,key);
  412. end;
  413. procedure Tsz_bm.FormShow(Sender: TObject);
  414. begin
  415.   dm.sz_bm.Close;
  416.   dm.sz_bm.CommandText:='select * from sz_bm';
  417.   dm.sz_bm.Open;
  418.   dm.xt_lbjs.Close;
  419.   dm.xt_lbjs.CommandText:='select lbjs_sm,lb_dmgz from xt_lbjs where lb_dm='+''''+'sz_bm'+''''+'';
  420.   dm.xt_lbjs.Open;
  421.   lbjs_sm:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
  422.   lb_dmgz:=dm.xt_lbjs.fieldbyname('lb_dmgz').AsString;
  423.   edit1.Text:=lb_dmgz;
  424.   //-----------------------------------------------
  425.    loadTree(dm.sz_bm);//初始化树
  426.    dm.sz_bm.Close;
  427.    dm.sz_bm.CommandText:='select * from sz_bm';
  428.    dm.sz_bm.Open;
  429. end;
  430. procedure Tsz_bm.TreeClick(Sender: TObject);
  431. var
  432.   sss:string;
  433.    s1,s2:string;
  434.    i,Epos:integer;
  435. begin
  436.   if tree.selected=nil then
  437.      exit
  438.   else
  439.  if Tree.Items[0].HasChildren<>false then
  440.  begin   //选中和不选中的图片显示
  441.     for i:=0 to Tree.items.count-1 do
  442.     begin
  443.       Tree.Items[i].ImageIndex:=0;
  444.       Tree.Items[i].SelectedIndex:=1;
  445.       s1:=Tree.Selected.Text;
  446.       Epos:=pos('-',s1);
  447.       s2:=copy(s1,0,Epos-1);
  448.       dm.sz_bm.Close;
  449.       dm.sz_bm.CommandText:='select * from sz_bm where bm_dm='+''''+s2+''''+'';
  450.       dm.sz_bm.Open;
  451.       dbedit1.Text:=dm.sz_bm.fieldbyname('bm_dm').AsString;
  452.       dbedit2.Text:=dm.sz_bm.fieldbyname('bm_mc').AsString;
  453.       dbedit3.Text:=dm.sz_bm.fieldbyname('bm_js').AsString;
  454.       dbedit4.Text:=dm.sz_bm.fieldbyname('bm_sm').AsString;
  455.     end;
  456.  end;
  457. end;
  458. end.