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

Delphi控件源码

开发平台:

Delphi

  1. unit sz_qzs;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, DB, XPBarMenu, Grids, DBGrids, StdCtrls, ComCtrls, ExtCtrls,
  6.   ToolWin, Mask, DBCtrls, ActnList, Menus, DBActns,
  7.   PrnDbgeh, DBGridEh, DBClient, XPMenu;
  8. type
  9.   Tsz_qz = class(TForm)
  10.     CoolBar1: TCoolBar;
  11.     ToolBar1: TToolBar;
  12.     add1: TToolButton;
  13.     ToolButton5: TToolButton;
  14.     ToolButton6: TToolButton;
  15.     ToolButton4: TToolButton;
  16.     Panel1: TPanel;
  17.     Splitter1: TSplitter;
  18.     Panel2: TPanel;
  19.     Tree: TTreeView;
  20.     Panel3: TPanel;
  21.     Splitter2: TSplitter;
  22.     Panel4: TPanel;
  23.     Panel5: TPanel;
  24.     GroupBox2: TGroupBox;
  25.     DataSource1: TDataSource;
  26.     ToolButton3: TToolButton;
  27.     Label1: TLabel;
  28.     DBEdit1: TDBEdit;
  29.     Label2: TLabel;
  30.     DBEdit2: TDBEdit;
  31.     Label3: TLabel;
  32.     DBEdit3: TDBEdit;
  33.     Label4: TLabel;
  34.     DBEdit4: TDBEdit;
  35.     Label5: TLabel;
  36.     DBEdit5: TDBEdit;
  37.     Label6: TLabel;
  38.     DBEdit6: TDBEdit;
  39.     Label7: TLabel;
  40.     DBEdit7: TDBEdit;
  41.     Label8: TLabel;
  42.     Label9: TLabel;
  43.     ActionList1: TActionList;
  44.     Add: TAction;
  45.     Edit: TAction;
  46.     Delete: TAction;
  47.     Refresh: TAction;
  48.     Print: TAction;
  49.     Quit: TAction;
  50.     ToolButton7: TToolButton;
  51.     Save: TAction;
  52.     Edit1: TEdit;
  53.     Label10: TLabel;
  54.     PopupMenu1: TPopupMenu;
  55.     N1: TMenuItem;
  56.     N3: TMenuItem;
  57.     N2: TMenuItem;
  58.     N5: TMenuItem;
  59.     N6: TMenuItem;
  60.     PrintDBGridEh1: TPrintDBGridEh;
  61.     DBGridEh1: TDBGridEh;
  62.     xt_jbls: TDataSource;
  63.     XPBarMenu1: TXPBarMenu;
  64.     procedure ToolButton4Click(Sender: TObject);
  65.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  66.     procedure QuitExecute(Sender: TObject);
  67.     procedure SaveExecute(Sender: TObject);
  68.     procedure FormCreate(Sender: TObject);
  69.     procedure DeleteExecute(Sender: TObject);
  70.     procedure AddExecute(Sender: TObject);
  71.     procedure DBEdit1KeyPress(Sender: TObject; var Key: Char);
  72.     procedure DBEdit2KeyPress(Sender: TObject; var Key: Char);
  73.     procedure DBEdit3KeyPress(Sender: TObject; var Key: Char);
  74.     procedure DBEdit4KeyPress(Sender: TObject; var Key: Char);
  75.     procedure DBEdit5KeyPress(Sender: TObject; var Key: Char);
  76.     procedure DBEdit6KeyPress(Sender: TObject; var Key: Char);
  77.     procedure RefreshExecute(Sender: TObject);
  78.     procedure EditExecute(Sender: TObject);
  79.     procedure N5Click(Sender: TObject);
  80.     procedure N6Click(Sender: TObject);
  81.     procedure TreeClick(Sender: TObject);
  82.     procedure DBGridEh1CellClick(Column: TColumnEh);
  83.     procedure PrintExecute(Sender: TObject);
  84.   private
  85.     { Private declarations }
  86.   public
  87.     { Public declarations }
  88.    procedure loadTree(treeDB:TClientDataSet);//初始化树
  89.   end;
  90. var
  91.   sz_qz: Tsz_qz;
  92.    SCodeFormat:string;   //库位代码结构
  93.    lbjs:string;
  94.    SFirstNodeTxt:string;  //首节点显示的文字
  95.    lb_dmgz:string;//代码规则
  96. implementation
  97. uses Dms,mainfrms,U_public;
  98. {$R *.dfm}
  99. //以下过程为装载树
  100. procedure Tsz_qz.loadTree(treeDb:TClientDataSet);//初始化树
  101. var
  102.   nodeId,nodeTxt:string;
  103.   level,chindex,cnode,num:integer;
  104.   mynode:array[0..6] of Ttreenode;
  105. begin
  106.  //初始化变量
  107.    SCodeFormat:=lbjs ;   //库位代码结构
  108.    SFirstNodeTxt:= '库位';  //首节点显示的文字
  109.    Screen.Cursor:=crHourGlass;
  110.    Tree.enabled:=true;
  111.    Tree.Items.Clear;
  112.    level:=0;
  113.    num:=1;
  114.  //设置根节点
  115.  mynode[level]:=Tree.Items.Add(Tree.TopItem,'全宗');
  116.  mynode[level].ImageIndex:=0;
  117.  mynode[level].SelectedIndex:=1;
  118.  //遍历数据表,利用编码字段记录排序规律,依次添加树节点
  119.  with treeDB do
  120.  begin
  121.     try
  122.       if not active then open;
  123.       first;
  124.       while not treedb.Eof do
  125.       begin
  126.         nodeId:=trim(fieldbyname('qz_dm').AsString);
  127.         nodeTxt:=nodeId+'-'+trim(fieldbyname('qz_mc').AsString);
  128.         level:=GetNodeLevel(SCodeFormat,nodeId);//获得节点层树
  129.         //再这里返回代码的层次数
  130.         if level>0 then
  131.         begin
  132.            //增加下一节点时,用添加子节点的方法可以轻松实现节点的层次关系
  133.            //注意,这里的父节点是用当前节点的上一级节点mynode[level-1]
  134.            mynode[level]:=tree.Items.AddChild(mynode[level-1],nodeTxt);
  135.            mynode[level].ImageIndex:=0;
  136.            mynode[level].SelectedIndex:=1;
  137.         end;//end if
  138.       next;//下一条纪录
  139.       end;//end while
  140.     finally;
  141.       close;
  142.     end;
  143.     mynode[0].Expand(false);
  144.     screen.Cursor:=crdefault;
  145.  end;//end with
  146. end;
  147. //--------------------------------装载树
  148. procedure Tsz_qz.ToolButton4Click(Sender: TObject);
  149. begin
  150.  if Tree.Selected.Text='全宗' then
  151.      Tree.Selected.Delete;
  152. end;
  153. procedure Tsz_qz.FormClose(Sender: TObject; var Action: TCloseAction);
  154. begin
  155.   dm.sz_qz.Active:=false;
  156.   dm.xt_lbjs.Active:=false;
  157.   action:=cafree;
  158.   sz_qz:=nil;
  159.   Tree.Free;
  160. end;
  161. procedure Tsz_qz.QuitExecute(Sender: TObject);
  162. begin
  163.  close;
  164. end;
  165. procedure Tsz_qz.SaveExecute(Sender: TObject);
  166. var
  167.   i,j:integer;
  168.   s1,s2:string;
  169.   subnode:TTreenode;//先建立一个TReeview使用的子对象
  170. begin
  171.  i:=length(edit1.Text);//编码规则
  172.  s1:=trim(dbedit1.Text);
  173.  s2:=s1+'-'+trim(dbedit2.Text);
  174.  if length(s1)<> i then
  175.  begin
  176.    application.MessageBox('编码规则不合要求','提示',mb_ok+mb_iconinformation);
  177.    exit;
  178.  end;
  179.  if trim(dbedit1.Text)='' then
  180.  begin
  181.    application.MessageBox('全宗代码不能为空','提示',mb_ok+mb_iconinformation);
  182.    exit;
  183.  end;
  184.   if trim(dbedit1.Text)='' then
  185.  begin
  186.    application.MessageBox('全宗代码不能为空','提示',mb_ok+mb_iconinformation);
  187.    exit;
  188.  end;
  189.  //以上对数据进行有效性验证  
  190.     //-----------------
  191.  dm.sz_qz.ApplyUpdates(0);
  192.  j:=dm.sz_qz.ApplyUpdates(0);
  193.  if j<>0 then
  194.  begin
  195.     application.MessageBox('数据保存失败','提示',mb_ok+mb_iconinformation);
  196.     exit;
  197.  end
  198.  else
  199.  begin
  200.     application.MessageBox('数据保存成功','提示',mb_ok+mb_iconinformation);
  201.     //------------------
  202.     tree.SetFocus;//在根目录下新建一个子标题
  203.     if DbEdit1.Color<>cl3dLight then
  204.     begin
  205.        subnode:=Tree.Items.AddChild(Tree.Items.GetFirstNode,s2);
  206.        //在根节点添加子节点
  207.        subnode.ImageIndex:=0;
  208.        subnode.SelectedIndex:=1;
  209.        //----------------
  210.     end
  211.     else
  212.     begin
  213.       Tree.Selected.EditText;//修改节点
  214.       Tree.Selected.Text:=DbEdit1.Text+'-'+DbEdit2.Text;
  215.     end;
  216.        dm.sz_qz.Close;
  217.        dm.sz_qz.CommandText:='select * from sz_qz';
  218.        dm.sz_qz.Open;
  219.        Dbedit1.Enabled:=true;
  220.        Dbedit1.Color:=clHighlightText;
  221.        add1.Enabled:=true;
  222.        ToolButton4.Enabled:=true;ToolButton5.Enabled:=true;
  223.        ToolButton6.Enabled:=true;ToolButton7.Enabled:=false;
  224.        N2.Enabled:=true;N1.Enabled:=true;N3.Enabled:=true;     
  225.  end;
  226. end;
  227. procedure Tsz_qz.FormCreate(Sender: TObject);
  228. var i:integer;
  229. begin
  230.   Dm.sz_qz.Close;
  231.   dm.sz_qz.CommandText:='select * from sz_qz';
  232.   dm.sz_qz.Open;
  233.   Dm.xt_lbjs.Close;
  234.   Dm.xt_lbjs.CommandText:='Select lb_dmgz,lbjs_sm from xt_lbjs where lb_dm='+''''+'sz_qz'+''''+'';
  235.   Dm.xt_lbjs.Open;
  236.   lbjs:=dm.xt_lbjs.fieldbyname('lbjs_sm').AsString;
  237.   Lb_dmgz:=dm.xt_lbjs.fieldbyname('lb_dmgz').asstring;
  238.   edit1.Text:=lb_dmgz;
  239.   loadtree(dm.sz_qz);
  240. end;
  241. procedure Tsz_qz.DeleteExecute(Sender: TObject);
  242. var s1,s2:string;
  243.     Epos:integer;
  244. begin
  245.    if Tree.Items[0].HasChildren<>false then
  246.    begin
  247.      if application.MessageBox('您确定要删除当前纪录吗?','警告',mb_okcancel+mb_iconquestion)=idok then
  248.      begin
  249.        s1:=Tree.Selected.Text;
  250.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  251.        s2:=copy(s1,0,Epos-1);//
  252.        tree.Selected.Delete;//删除树形表中数据
  253.        dm.sz_qz.Close;
  254.        dm.sz_qz.CommandText:='delete  from sz_qz where qz_dm='+''''+s2+''''+'';
  255.        dm.sz_qz.execute;
  256.        dm.sz_qz.Close;
  257.        dm.sz_qz.CommandText:='select * from sz_qz';
  258.        dm.sz_qz.Open;
  259.      end;
  260.    end
  261.    else
  262.    begin
  263.       application.MessageBox('当前表中没有数据,不能删除','提示',mb_ok+mb_iconinformation);
  264.       exit;
  265.    end;   
  266. end;
  267. procedure Tsz_qz.AddExecute(Sender: TObject);
  268. begin
  269.   Toolbutton5.Enabled:=false;//修改
  270.   Toolbutton6.Enabled:=false;//删除
  271.   Toolbutton7.Enabled:=true;//保存
  272.   N2.Enabled:=false;N1.Enabled:=false;N3.Enabled:=false;
  273.   dm.sz_qz.Close;
  274.   dm.sz_qz.CommandText:='select * from sz_qz where qz_dm='+''''+''''+'';
  275.   dm.sz_qz.Open;
  276.   tree.Enabled:=true;
  277. end;
  278. procedure Tsz_qz.DBEdit1KeyPress(Sender: TObject; var Key: Char);
  279. begin
  280.   if key=#13 then
  281.   dbedit2.SetFocus;
  282. end;
  283. procedure Tsz_qz.DBEdit2KeyPress(Sender: TObject; var Key: Char);
  284. begin
  285.   if key=#13 then
  286.      dbedit3.SetFocus;
  287. end;
  288. procedure Tsz_qz.DBEdit3KeyPress(Sender: TObject; var Key: Char);
  289. begin
  290.  if key=#13 then
  291.     dbedit4.SetFocus;
  292. end;
  293. procedure Tsz_qz.DBEdit4KeyPress(Sender: TObject; var Key: Char);
  294. begin
  295.    if key=#13 then
  296.       dbedit5.SetFocus;
  297. end;
  298. procedure Tsz_qz.DBEdit5KeyPress(Sender: TObject; var Key: Char);
  299. begin
  300.    if key=#13 then
  301.       dbedit6.SetFocus;
  302. end;
  303. procedure Tsz_qz.DBEdit6KeyPress(Sender: TObject; var Key: Char);
  304. begin
  305.   if key=#13 then
  306.      dbedit7.SetFocus;
  307. end;
  308. procedure Tsz_qz.RefreshExecute(Sender: TObject);
  309. begin
  310.  dm.sz_qz.Close;
  311.  dm.sz_qz.CommandText:='select * from sz_qz';
  312.  dm.sz_qz.Open;
  313.  if dm.sz_qz.RecordCount<>0 then
  314.   begin
  315.     Add1.Enabled:=true;ToolButton7.Enabled:=false;
  316.     ToolButton5.Enabled:=true;ToolButton6.Enabled:=true;
  317.   end
  318.  else
  319.   begin
  320.     add1.Enabled:=true;N1.Enabled:=true;
  321.   end;
  322. end;           
  323. procedure Tsz_qz.EditExecute(Sender: TObject);
  324. var s1,s2:string;
  325.     Epos:integer;
  326. begin
  327.    ToolButton7.Enabled:=true;toolbutton6.Enabled:=false;
  328.    Add1.Enabled:=false;Toolbutton5.enabled:=false;
  329.    N1.Enabled:=false;N3.Enabled:=false;N2.enabled:=false;
  330.    if tree.Items.Count<>0 then
  331.    if Tree.Items[0].HasChildren<>false then
  332.    begin
  333.        s1:=Tree.Selected.Text;
  334.        Epos:=pos('-',s1);//取得'-'在节点名中的位置
  335.        s2:=copy(s1,0,Epos-1);//
  336.        dm.sz_qz.Close;
  337.        dm.sz_qz.CommandText:='select * from sz_qz where qz_dm='+''''+s2+''''+'';
  338.        dm.sz_qz.Open;
  339.        Dbedit1.Enabled:=false;
  340.        DBEdit1.Color:=cl3DLight;
  341.    end
  342.    else
  343.    begin
  344.       application.MessageBox('当前表中没有数据,不能修改','提示',mb_ok+mb_iconinformation);
  345.       exit;
  346.    end;
  347. end;
  348. procedure Tsz_qz.N5Click(Sender: TObject);
  349. begin
  350.  if tree.Selected.HasChildren then
  351.  begin
  352.     if tree.Selected.Expanded then
  353.        application.MessageBox('已经处于展开状态','提示',mb_OK+mb_iconinformation)
  354.     else
  355.        tree.Selected.Expand(true);
  356.  end
  357.  else
  358.      application.MessageBox('当前节点没有子节点,不能展开','提示',mb_OK+mb_iconinformation);
  359. end;
  360. procedure Tsz_qz.N6Click(Sender: TObject);
  361. begin
  362.   if tree.Selected.HasChildren then
  363.     tree.Selected.Collapse(true)
  364.  else
  365.     application.MessageBox('当前节点没有子节点,不能叠起','提示',mb_OK+mb_iconinformation);
  366. end;
  367. procedure Tsz_qz.TreeClick(Sender: TObject);
  368. var
  369.    s1,s2:string;
  370.    i,Epos:integer;
  371. begin
  372.  if Tree.Items[0].HasChildren<>false then
  373.  begin   //选中和不选中的图片显示
  374.     for i:=0 to Tree.items.count-1 do
  375.     begin
  376.       s1:=Tree.Selected.Text;
  377.       Epos:=pos('-',s1);
  378.       s2:=copy(s1,0,Epos-1);
  379.       dm.sz_qz.Close;
  380.       dm.sz_qz.CommandText:='select * from sz_qz where qz_dm='+''''+s2+''''+'';
  381.       dm.sz_qz.Open;
  382.       ToolButton7.Enabled:=false;
  383.       ToolButton6.Enabled:=true;
  384.       ToolButton5.Enabled:=true;
  385.     end;
  386.  end;//end if
  387. end;
  388. procedure Tsz_qz.DBGridEh1CellClick(Column: TColumnEh);
  389. var
  390.   s1,s2:string;
  391.   i:integer;
  392. begin//--1单击每一行时,使树形控件也变化
  393.   s1:=DBGridEh1.Columns[0].DisplayText;
  394.   s2:=DBGridEh1.Columns[1].DisplayText;
  395.   Tree.FullExpand;
  396.   Tree.AlphaSort(true);
  397.   if Tree.Items[0].HasChildren<>false then
  398.   begin//end if
  399.     for i:=0 to Tree.items.count-1 do
  400.       if Tree.Items[i].Text=s1+'-'+s2 then
  401.      begin
  402.          Tree.Items[i].Selected:=true;
  403.          break;
  404.      end;//end for
  405.   end;//end if
  406. end;//--1
  407. procedure Tsz_qz.PrintExecute(Sender: TObject);
  408. begin
  409.  PrintDBGridEh1.Title.Text:='全宗管理';
  410.  PrintDBGridEh1.Preview;
  411.  printDBgridEh1.AfterGridText.text:=datetostr(date);
  412. end;
  413. end.