DBTreeSample2.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:8k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit DBTreeSample2;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   Buttons, fcdbtreeview, Db, DBTables, Grids, fcTreeView, DBGrids,
  6.   ComCtrls, StdCtrls, Mask, DBCtrls, ExtCtrls, fcScrollBar, fcButton,
  7.   fcImgBtn, fcShapeBtn,
  8.   fcOutlookList, fcDemoRichEdit, Wwdatsrc, Wwtable, Menus;
  9. type
  10.   TDMTreeViewForm2= class(TForm)
  11.     fcDBTreeView1: TfcDBTreeView;
  12.     CustomerDS: TDataSource;
  13.     CustomerTbl: TTable;
  14.     CustomerTblCustNo: TFloatField;
  15.     CustomerTblCompany: TStringField;
  16.     CustomerTblAddr1: TStringField;
  17.     CustomerTblAddr2: TStringField;
  18.     CustomerTblCity: TStringField;
  19.     CustomerTblState: TStringField;
  20.     CustomerTblZip: TStringField;
  21.     CustomerTblCountry: TStringField;
  22.     CustomerTblPhone: TStringField;
  23.     CustomerTblFAX: TStringField;
  24.     CustomerTblTaxRate: TFloatField;
  25.     CustomerTblContact: TStringField;
  26.     CustomerTblLastInvoiceDate: TDateTimeField;
  27.     CustomerTblLookupOrders: TIntegerField;
  28.     OrdersDS: TDataSource;
  29.     OrdersTbl: TTable;
  30.     OrdersTblOrderNo: TFloatField;
  31.     OrdersTblCustNo: TFloatField;
  32.     OrdersTblSaleDate: TDateTimeField;
  33.     OrdersTblShipDate: TDateTimeField;
  34.     OrdersTblEmpNo: TIntegerField;
  35.     OrdersTblShipToContact: TStringField;
  36.     OrdersTblShipToAddr1: TStringField;
  37.     OrdersTblShipToAddr2: TStringField;
  38.     OrdersTblShipToCity: TStringField;
  39.     OrdersTblShipToState: TStringField;
  40.     OrdersTblShipToZip: TStringField;
  41.     OrdersTblShipToCountry: TStringField;
  42.     OrdersTblShipToPhone: TStringField;
  43.     OrdersTblShipVIA: TStringField;
  44.     OrdersTblPO: TStringField;
  45.     OrdersTblTerms: TStringField;
  46.     OrdersTblPaymentMethod: TStringField;
  47.     OrdersTblItemsTotal: TCurrencyField;
  48.     OrdersTblTaxRate: TFloatField;
  49.     OrdersTblFreight: TCurrencyField;
  50.     OrdersTblAmountPaid: TCurrencyField;
  51.     ItemsDS: TDataSource;
  52.     ItemsTbl: TTable;
  53.     ItemsTblOrderNo: TFloatField;
  54.     ItemsTblItemNo: TFloatField;
  55.     ItemsTblPartNo: TFloatField;
  56.     ItemsTblQty: TIntegerField;
  57.     ItemsTblDiscount: TFloatField;
  58.     ItemsTblLookupPartDescription: TStringField;
  59.     ItemsTblLookupPartPrice: TFloatField;
  60.     LookupPartTbl: TTable;
  61.     LookupOrderTbl: TTable;
  62.     PageControl1: TPageControl;
  63.     CustomersTabSheet: TTabSheet;
  64.     OrdersTabSheet: TTabSheet;
  65.     ItemsTabSheet: TTabSheet;
  66.     Label1: TLabel;
  67.     Label2: TLabel;
  68.     EditCustNo: TDBEdit;
  69.     EditCompany: TDBEdit;
  70.     EditAddr: TDBEdit;
  71.     Label3: TLabel;
  72.     EditAddr2: TDBEdit;
  73.     Label4: TLabel;
  74.     EditZip: TDBEdit;
  75.     Label7: TLabel;
  76.     EditState: TDBEdit;
  77.     Label6: TLabel;
  78.     Label5: TLabel;
  79.     EditCity: TDBEdit;
  80.     Label8: TLabel;
  81.     EditCountry: TDBEdit;
  82.     EditPhone: TDBEdit;
  83.     Label9: TLabel;
  84.     Label10: TLabel;
  85.     EditFAX: TDBEdit;
  86.     EditLastInvoiceDate: TDBEdit;
  87.     Label13: TLabel;
  88.     EditContact: TDBEdit;
  89.     Label12: TLabel;
  90.     EditTaxRate: TDBEdit;
  91.     Label11: TLabel;
  92.     TreeStateLabel: TLabel;
  93.     ImageList1: TImageList;
  94.     Label14: TLabel;
  95.     EditOrderNo: TDBEdit;
  96.     DBEdit1: TDBEdit;
  97.     Label15: TLabel;
  98.     EditSaleDate: TDBEdit;
  99.     Label16: TLabel;
  100.     EditEmpNo: TDBEdit;
  101.     Label17: TLabel;
  102.     EditShipDate: TDBEdit;
  103.     Label18: TLabel;
  104.     EditShipVIA: TDBEdit;
  105.     Label25: TLabel;
  106.     EditPO: TDBEdit;
  107.     Label28: TLabel;
  108.     EditTerms: TDBEdit;
  109.     Label29: TLabel;
  110.     EditPaymentMethod: TDBEdit;
  111.     Label30: TLabel;
  112.     EditItemsTotal: TDBEdit;
  113.     Label31: TLabel;
  114.     EditAmountPaid: TDBEdit;
  115.     Label32: TLabel;
  116.     EditFreight: TDBEdit;
  117.     Label33: TLabel;
  118.     DBEdit2: TDBEdit;
  119.     Label34: TLabel;
  120.     EditDiscount: TDBEdit;
  121.     Label35: TLabel;
  122.     DBEdit3: TDBEdit;
  123.     Label36: TLabel;
  124.     Label37: TLabel;
  125.     EditItemNo: TDBEdit;
  126.     EditPartNo: TDBEdit;
  127.     Label38: TLabel;
  128.     EditQty: TDBEdit;
  129.     Label39: TLabel;
  130.     PartsDS: TDataSource;
  131.     PartsTbl: TTable;
  132.     GroupBox1: TGroupBox;
  133.     DBEdit6: TDBEdit;
  134.     Label42: TLabel;
  135.     DBEdit4: TDBEdit;
  136.     Label40: TLabel;
  137.     DBEdit5: TDBEdit;
  138.     Label41: TLabel;
  139.     DBEdit7: TDBEdit;
  140.     Label43: TLabel;
  141.     DBEdit8: TDBEdit;
  142.     Label44: TLabel;
  143.     DBEdit9: TDBEdit;
  144.     Label45: TLabel;
  145.     LabelsTbl: TwwTable;
  146.     TreeLabelsDS: TwwDataSource;
  147.     VendorsDS: TwwDataSource;
  148.     VendorsTbl: TwwTable;
  149.     VendorsTabSheet: TTabSheet;
  150.     Label21: TLabel;
  151.     DBEdit10: TDBEdit;
  152.     Label22: TLabel;
  153.     DBEdit11: TDBEdit;
  154.     Label23: TLabel;
  155.     DBEdit12: TDBEdit;
  156.     Label24: TLabel;
  157.     DBEdit13: TDBEdit;
  158.     Label26: TLabel;
  159.     DBEdit14: TDBEdit;
  160.     Label27: TLabel;
  161.     DBEdit15: TDBEdit;
  162.     Label46: TLabel;
  163.     DBEdit16: TDBEdit;
  164.     Label47: TLabel;
  165.     DBEdit17: TDBEdit;
  166.     Label48: TLabel;
  167.     DBEdit18: TDBEdit;
  168.     Label49: TLabel;
  169.     DBEdit19: TDBEdit;
  170.     DBCheckBox1: TDBCheckBox;
  171.     procedure fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  172.       Node: TfcDBTreeNode);
  173.     procedure PageControl1Change(Sender: TObject);
  174.     procedure fcDBTreeView1UserExpand(TreeView: TfcDBCustomTreeView;
  175.       Node: TfcDBTreeNode);
  176.     procedure fcDBTreeView1CalcNodeAttributes(
  177.       TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode);
  178.     procedure SpeedButton1Click(Sender: TObject);
  179.   private
  180.     procedure ChangeDataSources(TreeView: TfcDBCustomTreeView; RootLabel: string);
  181.     { Private declarations }
  182.   public
  183.     { Public declarations }
  184.   end;
  185. var
  186.   DMTreeViewForm2: TDMTreeViewForm2;
  187. implementation
  188. {$R *.DFM}
  189. procedure TDMTreeViewForm2.fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  190.   Node: TfcDBTreeNode);
  191. var s: string;
  192.     tempNode: TfcDBTreeNode;
  193. begin
  194.    if Node.Level=0 then ChangeDataSources(TreeView, Node.Text);
  195.   { Compute label to indicate tree state }
  196.   s:= '';
  197.   tempNode:= node;
  198.   repeat
  199.      s:= tempNode.Text + #13 + s;
  200.      tempNode:= tempNode.parent;
  201.   until tempNode=nil;
  202.   TreeStateLabel.caption:= s;
  203.   { Select appropriate tab sheet based on active node }
  204.   if Node.dataset=CustomerTbl then PageControl1.activePage:= CustomersTabSheet
  205.   else if node.dataset=OrdersTbl then PageControl1.activePage:= OrdersTabSheet
  206.   else if node.dataset=ItemsTbl then PageControl1.activePage:= ItemsTabSheet
  207.   else if node.dataset=VendorsTbl then PageControl1.activePage:= VendorsTabSheet;
  208. end;
  209. procedure TDMTreeViewForm2.PageControl1Change(Sender: TObject);
  210. begin
  211.    { This code causes the treeview to highlight the
  212.      dataset associated with the ActivePage of the tab control }
  213.    if PageControl1.ActivePage = OrdersTabSheet then
  214.    begin
  215.       LabelsTbl.Locate('TreeRootLabels', 'Customers', []);
  216.       fcDBTreeView1.MakeActiveDataSet(OrdersTbl, False)
  217.    end
  218.    else if PageControl1.ActivePage = ItemsTabSheet then
  219.    begin
  220.       LabelsTbl.Locate('TreeRootLabels', 'Customers', []);
  221.       fcDBTreeView1.MakeActiveDataSet(ItemsTbl, False)
  222.    end
  223.    else if PageControl1.ActivePage = CustomersTabSheet then
  224.    begin
  225.       LabelsTbl.Locate('TreeRootLabels', 'Customers', []);
  226.       fcDBTreeView1.MakeActiveDataSet(CustomerTbl, False)
  227.    end
  228.    else if PageControl1.ActivePage = VendorsTabSheet then
  229.    begin
  230.       LabelsTbl.Locate('TreeRootLabels', 'Vendors', []);
  231.       fcDBTreeView1.MakeActiveDataSet(VendorsTbl, False)
  232.    end;
  233. end;
  234. procedure TDMTreeViewForm2.fcDBTreeView1UserExpand(
  235.   TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode);
  236. begin
  237.    if Node.Level=0 then ChangeDataSources(TreeView, Node.Text)
  238. end;
  239. { Change current list of datasources based on the active root node (label) }
  240. procedure TDMTreeViewForm2.ChangeDataSources(TreeView: TfcDBCustomTreeView; RootLabel: string);
  241. begin
  242.    if RootLabel='Customers' then begin
  243.      TreeView.DataSources:= 'TreeLabelsDS;CustomerDS;OrdersDS;ItemsDS';
  244.      TreeView.DisplayFields[1]:= '"CustNo", "Company"';
  245.    end
  246.    else begin
  247.      TreeView.DataSources:= 'TreeLabelsDS;VendorsDS';
  248.      TreeView.DisplayFields[1]:= '"VendorName"';
  249.    end
  250. end;
  251. procedure TDMTreeViewForm2.fcDBTreeView1CalcNodeAttributes(
  252.   TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode);
  253. begin
  254.    if Node.DataSet = CustomerTbl then begin
  255.       { Use lookupfield to determine if expand icon (+) should be displayed }
  256.       Node.HasChildren:= not node.dataset.fieldbyname('LookupOrders').isNull;
  257.       { Use US bitmap for US customers }
  258.       if Node.DataSet.FieldByname('Country').asString ='US' then
  259.          Node.StateIndex:= 0
  260.       else Node.StateIndex:= 1;
  261.    end
  262. end;
  263. procedure TDMTreeViewForm2.SpeedButton1Click(Sender: TObject);
  264. begin
  265.   Customertbl.free;
  266. end;
  267. end.