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

Delphi控件源码

开发平台:

Delphi

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