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

Delphi控件源码

开发平台:

Delphi

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