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

Delphi控件源码

开发平台:

Delphi

  1. unit sz_kws;
  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,
  7.   DBTables, PrnDbgeh, DBGrids;
  8. type
  9.   Tsz_kw = class(TForm)
  10.     CoolBar1: TCoolBar;
  11.     ToolBar1: TToolBar;
  12.     add1: TToolButton;
  13.     xiugai: TToolButton;
  14.     dele1: TToolButton;
  15.     save1: TToolButton;
  16.     Refresh1: TToolButton;
  17.     ToolButton3: TToolButton;
  18.     Panel1: TPanel;
  19.     Splitter1: TSplitter;
  20.     Panel2: TPanel;
  21.     Panel3: TPanel;
  22.     Splitter2: TSplitter;
  23.     Panel4: TPanel;
  24.     Label8: TLabel;
  25.     Label9: TLabel;
  26.     Label10: TLabel;
  27.     Label1: TLabel;
  28.     Label2: TLabel;
  29.     Label3: TLabel;
  30.     Label4: TLabel;
  31.     Edit1: TEdit;
  32.     Panel5: TPanel;
  33.     GroupBox2: TGroupBox;
  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.     DBGridEh1: TDBGridEh;
  52.     dbEdit1: TEdit;
  53.     DbEdit2: TEdit;
  54.     DbEdit3: TEdit;
  55.     DbEdit4: TEdit;
  56.     ClientDataSet1: TClientDataSet;
  57.     procedure QuitExecute(Sender: TObject);
  58.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  59.     procedure AddExecute(Sender: TObject);
  60.     procedure EditExecute(Sender: TObject);
  61.     procedure SaveExecute(Sender: TObject);
  62.     procedure DeleteExecute(Sender: TObject);
  63.     procedure DBGridEh1CellClick(Column: TColumnEh);
  64.     procedure RefreshExecute(Sender: TObject);
  65.     procedure FormShow(Sender: TObject);
  66.     procedure dbEdit1KeyPress(Sender: TObject; var Key: Char);
  67.     procedure DbEdit2KeyPress(Sender: TObject; var Key: Char);
  68.     procedure TreeClick(Sender: TObject);
  69.     procedure TreeChange(Sender: TObject; Node: TTreeNode);
  70.     procedure N5Click(Sender: TObject);
  71.     procedure N6Click(Sender: TObject);
  72.   private
  73.     { Private declarations }
  74.   public
  75.     { Public declarations }
  76.     procedure  loadTree(treeDb:TClientDataSet);//初始化树
  77.     procedure BtnState();//增,改时的按钮变化
  78.     procedure SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
  79.   end;
  80. var
  81.   sz_kw: Tsz_kw;
  82.   gnode:TtreeNode;//当前节点
  83.   lbjs_sm:string;//类别级数
  84.   lb_dmgz:string;//代码规则
  85.   dsState:string;//确定是增,删,改
  86. implementation
  87. uses mainfrms,dms,u_public, ljszs;
  88. {$R *.dfm}
  89. procedure Tsz_kw.BtnState();
  90. begin
  91.   if add1.Enabled=true or xiugai.Enabled=true then
  92.   begin
  93.     add1.Enabled:=false;A1.Enabled:=false;
  94.     xiugai.Enabled:=false;E1.Enabled:=false;
  95.     dele1.Enabled:=false;D1.Enabled:=false;
  96.     save1.Enabled:=true;
  97.     refresh1.Enabled:=true;
  98.   end
  99.   else
  100.   begin
  101.     add1.Enabled:=true;A1.Enabled:=true;
  102.     xiugai.Enabled:=true;E1.Enabled:=true;
  103.     dele1.Enabled:=true;D1.Enabled:=true;
  104.     save1.Enabled:=false;
  105.     refresh1.Enabled:=true;
  106.   end;
  107. end;
  108. procedure Tsz_kw.SaveData(TreeView:TtreeView;edit1,edit2,edit4:Tedit);//保存数据
  109. var
  110.   curnode:TTreenode;//已选树节点
  111.   Pnodetxt,nodetxt:string;//Pnodetxt:取出树节点的代码;nodetxt:要在树节点上增加的值
  112.   n:integer;//取出'-'在已选树节点中的位置
  113.   Tdbedit3:Tedit;
  114. begin
  115.    dm.sz_kw.edit;
  116.    n:=pos('-',TreeView.Selected.Text)-1;
  117.    Pnodetxt:=copy(TreeView.Selected.Text,1,n);
  118.    nodetxt:=Pnodetxt+dbedit1.Text+'-'+dbedit2.Text;//现在节点等于原来的加上现在的
  119.    //增加数据到节点
  120.    dm.sz_kw.FieldByName('kw_dm').AsString:=Pnodetxt+dbedit1.Text;
  121.    dm.sz_kw.FieldByName('kw_mc').AsString:=dbedit2.Text;
  122.    dm.sz_kw.FieldByName('kw_js').AsInteger:=tree.Selected.Level;
  123.    dm.sz_kw.FieldByName('kw_sm').AsString:=dbEdit4.Text;
  124.    dm.sz_kw.FieldByName('kw_sjkwdm').AsString:=Pnodetxt;
  125.    //给数组赋值
  126.    dm.sz_kw.ApplyUpdates(0);
  127.    application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  128.    curnode:=TreeView.Items.AddChild(tree.Selected,nodetxt);//增加新的节点
  129.    curnode.ImageIndex:=0;
  130.    curnode.SelectedIndex:=1;
  131.    //-------按钮状态
  132.    dbedit1.Text:='';
  133.    dbedit2.Text:='';
  134.    dbedit3.Text:='';
  135.    dbedit4.Text:='';
  136.    dsState:='';
  137. end;
  138. {当然,本文的核心是LoadTree过程,该函数用了一个循环来遍历数据表sz_kw的所有记录,
  139. 将字段kw_dm和kw_mc的内容按层次显示出来。而在该函数中定义的二维数组MyNode[0..6],
  140. 则显得优为重要,在这里,它作用类似于递归中的栈。因为在TTreeView 添加子节点的方
  141. 法AddChild(Node: TTreeNode; const S: string)中,要为其指定一父节点作为参数,而
  142. 父节点的代码级数一定是要添加节点的代码级数减1,所以只要用一数组来动态保存和指定
  143. 父节点就成功了}
  144. //以下函数是本文的重点部分,其主要功能是用一循环将sz_kw表中的
  145. //以下函数将库位表中的库位代码和库位代码名称显示出来
  146. procedure Tsz_kw.loadTree(treeDb:TClientDataSet);//初始化树
  147. var
  148.   nodeId,nodeTxt:string;
  149.   SCodeFormat,SFirstNodeTxt:string;
  150.   level:integer;
  151.   mynode:array[0..6] of Ttreenode;
  152. begin
  153.  //初始化变量
  154.    SCodeFormat:=lbjs_sm ;   //库位代码结构
  155.    SFirstNodeTxt:= '库位';  //首节点显示的文字
  156.    Screen.Cursor:=crHourGlass;
  157.    Tree.enabled:=true;
  158.    tree.Items.Clear;
  159.    level:=0;
  160.  //设置根节点
  161.  mynode[level]:=tree.Items.Add(tree.TopItem,'库位');
  162.  mynode[level].ImageIndex:=0;
  163.  mynode[level].SelectedIndex:=1;
  164.  //遍历数据表,利用编码字段记录排序规律,依次添加树节点
  165.  with treeDB do
  166.  begin
  167.     try
  168.       if not active then open;
  169.       first;
  170.       while not treedb.Eof do
  171.       begin
  172.         nodeId:=trim(fieldbyname('kw_dm').AsString);
  173.         nodeTxt:=nodeId+'-'+trim(fieldbyname('kw_mc').AsString);
  174.         level:=GetNodeLevel(SCodeFormat,nodeId);//获得节点层树
  175.         //再这里返回代码的层次数
  176.         if level>0 then
  177.         begin
  178.            //增加下一节点时,用添加子节点的方法可以轻松实现节点的层次关系
  179.            //注意,这里的父节点是用当前节点的上一级节点mynode[level-1]
  180.            mynode[level]:=tree.Items.AddChild(mynode[level-1],nodeTxt);
  181.            mynode[level].ImageIndex:=0;
  182.            mynode[level].SelectedIndex:=1;
  183.         end;//end if
  184.       next;//下一条纪录
  185.       end;//end while
  186.     finally;
  187.       close;
  188.     end;
  189.     mynode[0].Expand(false);
  190.     screen.Cursor:=crdefault;
  191.  end;//end with
  192. end;
  193. procedure Tsz_kw.QuitExecute(Sender: TObject);
  194. begin
  195.  close;
  196. end;
  197. procedure Tsz_kw.FormClose(Sender: TObject; var Action: TCloseAction);
  198. begin
  199.   dm.sz_kw.Active:=false;
  200.   dm.xt_lbjs.active:=false;
  201.   action:=cafree;
  202.   sz_kw:=nil;
  203.   tree.Free;
  204. end;
  205. procedure Tsz_kw.AddExecute(Sender: TObject);
  206. begin
  207.   if tree.Selected=nil then
  208.       exit
  209.   else
  210.   dm.sz_kw.Close;
  211.   dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+''''+'';
  212.   dm.sz_kw.Open;
  213.   if tree.Selected.level>length(lbjs_sm)-1 then
  214.   begin
  215.      application.MessageBox('已经到达最底层的节点,不能再增加','提示',mb_ok+mb_iconinformation);
  216.      exit;
  217.   end;
  218.   dbedit1.SetFocus;
  219.   dbedit1.Text:='';
  220.   dbedit2.text:='';
  221.   dbedit4.Text:='';
  222.   dbedit3.Text:=IntToStr(tree.Selected.level);
  223.   BtnState();//调用按钮的状态参数
  224.   dsState:='add'; 
  225. end;
  226. procedure Tsz_kw.EditExecute(Sender: TObject);//修改数据
  227. var s1,s2:string;
  228.     Epos:integer;
  229. begin
  230.    if tree.selected=nil then
  231.       exit
  232.     else  
  233.    if tree.Items.Count<>0 then
  234.    if Tree.Items[0].HasChildren<>false then
  235.    begin
  236.        BtnState();//调用按钮的状态参数
  237.        s1:=Tree.Selected.Text;
  238.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  239.        s2:=copy(s1,0,Epos-1);//
  240.        dm.sz_kw.Close;
  241.        dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+s2+''''+'';
  242.        dm.sz_kw.Open;
  243.        dbedit1.Text:=dm.sz_kw.fieldbyname('kw_dm').AsString;
  244.        Dbedit1.Enabled:=false;
  245.        DBEdit1.Color:=cl3DLight;
  246.        dm.sz_kw.edit;
  247.    end
  248.    else
  249.    begin
  250.       application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
  251.       exit;
  252.    end;
  253. end;
  254. procedure Tsz_kw.SaveExecute(Sender: TObject);
  255. var
  256.   s_sj,yanzheng:string;//yangzheng查找数据库中是否有重复记录存在
  257.   Pnodetxt:string;//pnodetxt:父节点值
  258.   n,n_sj:integer;
  259. begin
  260.   if dbedit1.Text='' then
  261.    begin
  262.     application.MessageBox('库位代码不能为空','提示',mb_ok+mb_iconinformation);
  263.     exit;
  264.    end;
  265.   if dbedit2.Text='' then
  266.    begin
  267.     application.MessageBox('库位名称不能为空','提示',mb_ok+mb_iconinformation);
  268.     exit;
  269.    end;
  270.   //---------------------验证必须输入的数据
  271.   //判断是增加还是修改*****************************************************
  272.     if dbedit1.Enabled=true then
  273.      begin
  274.      if tree.Selected.Level=length(lbjs_sm) then
  275.       begin
  276.         application.MessageBox('已经到达最底层节点'#13#10'不能增加','提示',mb_ok+mb_iconinformation);
  277.         exit;
  278.       end  else
  279.       case tree.Selected.Level of
  280.       //************************************************************************
  281.       0:   begin
  282.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,1,1)) then
  283.                begin
  284.                  hint:='请输入'+copy(lbjs_sm,1,1)+'位代码';
  285.                  application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  286.                  exit;
  287.                end
  288.              else//
  289.                begin
  290.                 yanzheng:=dbedit1.Text;
  291.                 dm.sz_kw.Close;
  292.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  293.                 dm.sz_kw.Open;
  294.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  295.                 begin
  296.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  297.                    exit;
  298.                 end
  299.                 else
  300.                 begin
  301.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  302.                    BtnState();//调用按钮的状态参数
  303.                 end;
  304.              end;//end if 0
  305.            end;
  306.       //************************************************************************
  307.       1:   begin
  308.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,2,1)) then
  309.               begin
  310.                  hint:='请输入'+copy(lbjs_sm,2,1)+'位代码';
  311.                  application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  312.                  exit;
  313.               end
  314.              else//1111111111111111111111111111111111111111111111111111111111111
  315.                begin//else begin
  316.                 n_sj:=pos('-',tree.Selected.Text)-1;
  317.                 s_sj:=copy(tree.Selected.Text,0,n_sj);
  318.                 yanzheng:=s_sj+dbedit1.text;//节点含有子节点
  319.                 dm.sz_kw.Close;
  320.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  321.                 dm.sz_kw.Open;
  322.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  323.                  begin
  324.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  325.                    exit;
  326.                  end
  327.                 else
  328.                  begin//@@@@
  329.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  330.                    BtnState();//调用按钮的状态参数
  331.                  end;//@@@@
  332.              end;//end else begin
  333.          end;//end  1
  334.       //************************************************************************
  335.       2:   begin
  336.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,3,1)) then
  337.              begin
  338.                 hint:='请输入'+copy(lbjs_sm,3,1)+'位代码';
  339.                 application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  340.                 exit;
  341.              end
  342.              else
  343.              begin//else begin
  344.                 n_sj:=pos('-',tree.Selected.Text)-1;
  345.                 s_sj:=copy(tree.Selected.Text,0,n_sj);
  346.                 yanzheng:=s_sj+dbedit1.text;//节点含有子节点
  347.                 dm.sz_kw.Close;
  348.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  349.                 dm.sz_kw.Open;
  350.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  351.                  begin
  352.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  353.                    exit;
  354.                  end
  355.                 else
  356.                  begin//@@@@
  357.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  358.                    BtnState();//调用按钮的状态参数
  359.                  end;//@@@@
  360.              end;
  361.            end;
  362.       3:   begin
  363.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,4,1)) then
  364.               begin
  365.                  hint:='请输入'+copy(lbjs_sm,4,1)+'位代码';
  366.                  application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  367.                  exit;
  368.               end
  369.              else
  370.               begin
  371.               n_sj:=pos('-',tree.Selected.Text)-1;
  372.                 s_sj:=copy(tree.Selected.Text,0,n_sj);
  373.                 yanzheng:=s_sj+dbedit1.text;//节点含有子节点
  374.                 dm.sz_kw.Close;
  375.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  376.                 dm.sz_kw.Open;
  377.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  378.                  begin
  379.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  380.                    exit;
  381.                  end
  382.                 else
  383.                  begin//@@@@
  384.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  385.                    BtnState();//调用按钮的状态参数
  386.                  end;//@@@@
  387.               end;
  388.            end;
  389.       4:   begin
  390.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,5,1)) then
  391.               begin
  392.                 hint:='请输入'+copy(lbjs_sm,5,1)+'位代码';
  393.                 application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  394.                 exit;
  395.               end
  396.              else
  397.               begin
  398.                 n_sj:=pos('-',tree.Selected.Text)-1;
  399.                 s_sj:=copy(tree.Selected.Text,0,n_sj);
  400.                 yanzheng:=s_sj+dbedit1.text;//节点含有子节点
  401.                 dm.sz_kw.Close;
  402.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  403.                 dm.sz_kw.Open;
  404.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  405.                  begin
  406.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  407.                    exit;
  408.                  end
  409.                 else
  410.                  begin//@@@@
  411.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  412.                    BtnState();//调用按钮的状态参数
  413.                  end;//@@@@
  414.               end;
  415.            end;
  416.       5:   begin
  417.              if length(dbedit1.Text)<>strtoInt(copy(lbjs_sm,6,1)) then
  418.               begin
  419.                 hint:='请输入'+copy(lbjs_sm,6,1)+'位代码';
  420.                 application.MessageBox(Pchar(hint),'提示',mb_okcancel+mb_iconinformation);
  421.                 exit;
  422.               end
  423.              else
  424.               begin
  425.                 n_sj:=pos('-',tree.Selected.Text)-1;
  426.                 s_sj:=copy(tree.Selected.Text,0,n_sj);
  427.                 yanzheng:=s_sj+dbedit1.text;//节点含有子节点
  428.                 dm.sz_kw.Close;
  429.                 dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+yanzheng+''''+'';
  430.                 dm.sz_kw.Open;
  431.                 if dm.sz_kw.FieldByName('kw_dm').AsString<>'' then
  432.                  begin
  433.                    application.MessageBox('已经有重复纪录存在','提示',mb_ok+mb_iconinformation);
  434.                    exit;
  435.                  end
  436.                 else
  437.                  begin//@@@@
  438.                    SaveData(tree,dbedit1,dbedit2,dbedit4);//调用保存过程
  439.                    //refresh1.Click;
  440.                    BtnState();//调用按钮的状态参数
  441.                  end;//@@@@
  442.               end;
  443.            end;//end 5
  444.       end;//end case
  445.     end//结束增加的情况
  446.     else
  447.      begin //修改后的情况
  448.       dm.sz_kw.edit;
  449.       Tree.Selected.EditText;//修改节点
  450.       Tree.Selected.Text:=Dbedit1.text+'-'+DbEdit2.Text;
  451.       tree.Selected.ImageIndex:=0;
  452.       tree.Selected.SelectedIndex:=1;
  453.       //增加数据到节点
  454.       dm.sz_kw.FieldByName('kw_dm').AsString:=dbedit1.Text;
  455.       dm.sz_kw.FieldByName('kw_mc').AsString:=dbedit2.Text;
  456.       dm.sz_kw.FieldByName('kw_js').AsInteger:=tree.Selected.Level;
  457.       dm.sz_kw.FieldByName('kw_sm').AsString:=dbEdit4.Text;
  458.       //给树赋值
  459.        dm.sz_kw.ApplyUpdates(0);
  460.        application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  461.        //refresh1.Click;//刷新
  462.        BtnState();
  463.        dbedit1.Enabled:=true;
  464.        dbedit1.Color:=clwindow;
  465.      end;
  466. end;
  467. procedure Tsz_kw.DeleteExecute(Sender: TObject);
  468. var
  469.    epos:integer;
  470.    s1,s2:string;
  471. begin
  472.   if tree.Selected=nil then
  473.      exit
  474.   else   
  475.   if tree.Items[0].HasChildren then
  476.    if tree.Selected.HasChildren=true then
  477.     begin
  478.      application.messagebox('该部门包含下级,不能删除','提示',MB_OKCANCEL+mb_iconinformation );
  479.     end
  480.     else
  481.     begin
  482.      if application.messagebox('您确实要删除当前记录吗?','提示',MB_OKCANCEL+mb_iconquestion )=IDOK then
  483.      begin
  484.        s1:=Tree.Selected.Text;
  485.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  486.        s2:=copy(s1,0,Epos-1);//
  487.        tree.Selected.Delete;//删除树形表中数据
  488.      //--------------------------------------
  489.        dbedit1.Text:='';dbedit2.text:='';dbedit4.Text:='';
  490.        Clientdataset1.Close;
  491.        Clientdataset1.CommandText:='select * from sz_kw';
  492.        Clientdataset1.Open;//
  493.        if Clientdataset1.recordcount=0 then
  494.          begin
  495.            dele1.Enabled:=false;
  496.            xiugai.Enabled:=false;
  497.          end;//--------------------------
  498.        dm.sz_kw.Close;
  499.        dm.sz_kw.CommandText:='delete  from sz_kw where kw_dm='+''''+s2+''''+'';
  500.        dm.sz_kw.execute;
  501.        dm.sz_kw.Close;
  502.        dm.sz_kw.CommandText:='select * from sz_kw';
  503.        dm.sz_kw.Open;
  504.        //删除表中数据
  505.      end;
  506.    end;
  507. end;
  508. procedure Tsz_kw.DBGridEh1CellClick(Column: TColumnEh);
  509. var
  510.   s1,s2:string;
  511.   i:integer;
  512. begin//--1单击每一行时,使树形控件也变化
  513.   s1:=DBGridEh1.Columns[1].DisplayText;
  514.   s2:=DBGridEh1.Columns[2].DisplayText;
  515.   dm.sz_kw.Locate('kw_dm','+''''+s1+''''+',[]);
  516.   //过滤要搜索的数据
  517.   dbedit1.Text:=dm.sz_kw.fieldbyname('kw_dm').AsString;
  518.   dbedit2.Text:=dm.sz_kw.fieldbyname('kw_mc').AsString;
  519.   dbedit3.Text:=dm.sz_kw.fieldbyname('kw_js').AsString;
  520.   dbedit4.Text:=dm.sz_kw.fieldbyname('kw_sm').AsString;
  521.   if Tree.Items[0].HasChildren<>false then
  522.   begin//end if
  523.     for i:=0 to Tree.items.count-1 do
  524.       if Tree.Items[i].Text=s1+'-'+s2 then
  525.      begin
  526.          Tree.Items[i].Selected:=true;
  527.          break;
  528.      end;//end for
  529.   end;//end if
  530. end;//--1
  531. procedure Tsz_kw.RefreshExecute(Sender: TObject);
  532. begin
  533.   dm.sz_kw.Close;
  534.   dm.sz_kw.CommandText:='select * from sz_kw';
  535.   dm.sz_kw.Open;
  536.   if dm.sz_kw.RecordCount<>0 then
  537.    begin
  538.      Add1.Enabled:=true;xiugai.Enabled:=true;Dele1.Enabled:=true;
  539.      save1.Enabled:=false;A1.Enabled:=true;E1.Enabled:=true;D1.Enabled:=true;
  540.    end
  541.   else
  542.    begin
  543.      Add1.Enabled:=true;xiugai.Enabled:=false;Dele1.Enabled:=false;
  544.      save1.Enabled:=false;A1.Enabled:=true;E1.Enabled:=false;D1.Enabled:=false;
  545.    end;
  546. end;
  547. procedure Tsz_kw.FormShow(Sender: TObject);
  548. begin
  549.    dm.sz_kw.Close;
  550.    dm.sz_kw.CommandText:='select * from sz_kw';
  551.    dm.sz_kw.Open;
  552.    dm.xt_lbjs.Close;
  553.    dm.xt_lbjs.CommandText:='select lbjs_sm,lb_dmgz from xt_lbjs where lb_dm='+''''+'sz_kw'+''''+'';
  554.    dm.xt_lbjs.Open;
  555.    //-----------------------------------------------
  556.    lbjs_sm:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
  557.    lb_dmgz:=dm.xt_lbjs.fieldbyname('lb_dmgz').asstring;
  558.    edit1.Text:=lb_dmgz;
  559.    loadTree(dm.sz_kw);//初始化树
  560.    dm.sz_kw.Close;
  561.    dm.sz_kw.CommandText:='select * from sz_kw';
  562.    dm.sz_kw.Open;
  563. end;
  564. procedure Tsz_kw.dbEdit1KeyPress(Sender: TObject; var Key: Char);
  565. begin
  566.   if key=#13 then
  567.   begin
  568.    key:=#0;
  569.    SelectNext(ActiveControl,true,true);
  570.   end;
  571. end;
  572. procedure Tsz_kw.DbEdit2KeyPress(Sender: TObject; var Key: Char);
  573. begin
  574.   dbedit1keypress(nil,key);
  575. end;
  576. procedure Tsz_kw.TreeClick(Sender: TObject);
  577. var
  578.    s1,s2:string;
  579.    i,Epos:integer;
  580. begin
  581.   if tree.selected=nil then
  582.      exit
  583.   else    
  584.  if Tree.Items[0].HasChildren<>false then
  585.  begin   //选中和不选中的图片显示
  586.     for i:=0 to Tree.items.count-1 do
  587.     begin
  588.       Tree.Items[i].ImageIndex:=0;
  589.       Tree.Items[i].SelectedIndex:=1;
  590.       s1:=Tree.Selected.Text;
  591.       Epos:=pos('-',s1);
  592.       s2:=copy(s1,0,Epos-1);
  593.       dm.sz_kw.Close;
  594.       dm.sz_kw.CommandText:='select * from sz_kw where kw_dm='+''''+s2+''''+'';
  595.       dm.sz_kw.Open;
  596.       dbedit1.Text:=dm.sz_kw.fieldbyname('kw_dm').AsString;
  597.       dbedit2.Text:=dm.sz_kw.fieldbyname('kw_mc').AsString;
  598.       dbedit3.Text:=dm.sz_kw.fieldbyname('kw_js').AsString;
  599.       dbedit4.Text:=dm.sz_kw.fieldbyname('kw_sm').AsString;
  600.     end;
  601.  end;
  602. end;
  603. procedure Tsz_kw.TreeChange(Sender: TObject; Node: TTreeNode);
  604. begin
  605.    if dsState='add' then
  606.      if application.MessageBox('请输入数据','提示',mb_okcancel+mb_iconinformation)=idOk then
  607.        //dbedit1.SetFocus
  608.        add.Execute
  609.      else
  610.      begin
  611.        BtnState();//确定按钮变回来
  612.      end;
  613. end;
  614. procedure Tsz_kw.N5Click(Sender: TObject);
  615. begin
  616.   tree.Selected.Expand(true);
  617. end;
  618. procedure Tsz_kw.N6Click(Sender: TObject);
  619. begin
  620.   tree.Selected.Collapse(true);
  621. end;
  622. end.