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

Delphi控件源码

开发平台:

Delphi

  1. unit DBTreeSample1;
  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, fcImager;
  9. type
  10.   TDMTreeViewForm = class(TForm)
  11.     fcDBTreeView1: TfcDBTreeView;
  12.     CustomersDS: TDataSource;
  13.     CustomersTbl: TTable;
  14.     CustomersTblCustNo: TFloatField;
  15.     CustomersTblCompany: TStringField;
  16.     CustomersTblAddr1: TStringField;
  17.     CustomersTblAddr2: TStringField;
  18.     CustomersTblCity: TStringField;
  19.     CustomersTblState: TStringField;
  20.     CustomersTblZip: TStringField;
  21.     CustomersTblCountry: TStringField;
  22.     CustomersTblPhone: TStringField;
  23.     CustomersTblFAX: TStringField;
  24.     CustomersTblTaxRate: TFloatField;
  25.     CustomersTblContact: TStringField;
  26.     CustomersTblLastInvoiceDate: TDateTimeField;
  27.     CustomersTblLookupOrders: 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.     SpeedButton1: TSpeedButton;
  146.     PageControl2: TPageControl;
  147.     TabSheet1: TTabSheet;
  148.     TabSheet2: TTabSheet;
  149.     CheckBox1: TCheckBox;
  150.     ComboBox1: TComboBox;
  151.     Label19: TLabel;
  152.     CheckBox2: TCheckBox;
  153.     CheckBox3: TCheckBox;
  154.     FilterCombo: TComboBox;
  155.     Label20: TLabel;
  156.     CheckBox4: TCheckBox;
  157.     fcDemoRichEdit1: TfcDemoRichEdit;
  158.     fcDemoRichEdit2: TfcDemoRichEdit;
  159.     CheckBox5: TCheckBox;
  160.     fcImager1: TfcImager;
  161.     procedure fcDBTreeView1CalcNodeAttributes(TreeView: TfcDBCustomTreeView;
  162.       Node: TfcDBTreeNode);
  163.     procedure fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  164.       Node: TfcDBTreeNode);
  165.     procedure PageControl1Change(Sender: TObject);
  166.     procedure ComboBox1Change(Sender: TObject);
  167.     procedure CheckBox1Click(Sender: TObject);
  168.     procedure CheckBox2Click(Sender: TObject);
  169.     procedure CheckBox3Click(Sender: TObject);
  170.     procedure FilterComboChange(Sender: TObject);
  171.     procedure CustomersTblFilterRecord(DataSet: TDataSet; var Accept: Boolean);
  172.     procedure CheckBox4Click(Sender: TObject);
  173.     procedure CheckBox5Click(Sender: TObject);
  174.     procedure fcDBTreeView1DrawText(TreeView: TfcDBCustomTreeView;
  175.       Node: TfcDBTreeNode; ARect: TRect; var DefaultDrawing: Boolean);
  176.   private
  177.     { Private declarations }
  178.   public
  179.     { Public declarations }
  180.   end;
  181. var
  182.   DMTreeViewForm: TDMTreeViewForm;
  183. implementation
  184. {$R *.DFM}
  185. procedure TDMTreeViewForm.fcDBTreeView1CalcNodeAttributes(TreeView: TfcDBCustomTreeView;
  186.   Node: TfcDBTreeNode);
  187. begin
  188.      //   TreeView.Canvas.Brush.Color:= clNone;
  189.    if Node.DataSet = CustomersTbl then begin
  190.       { Use lookupfield to determine if expand icon (+) should be displayed }
  191.       Node.HasChildren:= not node.dataset.fieldbyname('LookupOrders').isNull;
  192.       { Use US bitmap for US customers }
  193.       if Node.DataSet.FieldByname('Country').asString ='US' then
  194.          Node.StateIndex:= 0
  195.       else Node.StateIndex:= 1;
  196.    end
  197. end;
  198. procedure TDMTreeViewForm.fcDBTreeView1Change(TreeView: TfcDBCustomTreeView;
  199.   Node: TfcDBTreeNode);
  200. var s: string;
  201.     tempNode: TfcDBTreeNode;
  202. begin
  203.   { Compute label to indicate tree state }
  204.   s:= '';
  205.   tempNode:= node;
  206.   repeat
  207.      s:= tempNode.Text + #13 + s;
  208.      tempNode:= tempNode.parent;
  209.   until tempNode=nil;
  210.   TreeStateLabel.caption:= s;
  211.   { Select appropriate tab sheet based on active node }
  212.   if Node.dataset=CustomersTbl then PageControl1.activePage:= CustomersTabSheet
  213.   else if node.dataset=OrdersTbl then PageControl1.activePage:= OrdersTabSheet
  214.   else if node.dataset=ItemsTbl then PageControl1.activePage:= ItemsTabSheet;
  215. end;
  216. procedure TDMTreeViewForm.PageControl1Change(Sender: TObject);
  217. begin
  218.    { This code causes the treeview to highlight the
  219.      dataset associated with the ActivePage of the tab control }
  220.    if PageControl1.ActivePage = OrdersTabSheet then
  221.       fcDBTreeView1.MakeActiveDataSet(OrdersTbl, False)
  222.    else if PageControl1.ActivePage = ItemsTabSheet then
  223.       fcDBTreeView1.MakeActiveDataSet(ItemsTbl, False)
  224.    else if PageControl1.ActivePage = CustomersTabSheet then
  225.       fcDBTreeView1.MakeActiveDataSet(CustomersTbl, False)
  226. end;
  227. procedure TDMTreeViewForm.ComboBox1Change(Sender: TObject);
  228. begin
  229.    { Update treeview's fields based on active index }
  230.    with (Sender as TComboBox) do
  231.    begin
  232.       if text = 'By Company' then begin
  233.          CustomersTbl.indexname:= 'ByCompany';
  234.          fcDBTreeView1.DisplayFields[0]:= '"Company" ("CustNo")';
  235.       end
  236.       else begin
  237.          CustomersTbl.indexname:= '';
  238.          fcDBTreeView1.DisplayFields[0]:= '"CustNo", "Company"';
  239.       end;
  240.    end;
  241. end;
  242. procedure TDMTreeViewForm.CheckBox1Click(Sender: TObject);
  243. begin
  244.   with fcDBTreeView1 do begin
  245.      if dtvoExpandButtons3D in Options then
  246.         Options:= Options - [dtvoExpandButtons3D]
  247.      else Options:= Options + [dtvoExpandButtons3D]
  248.   end
  249. end;
  250. procedure TDMTreeViewForm.CheckBox2Click(Sender: TObject);
  251. begin
  252.   with fcDBTreeView1 do begin
  253.      if (Sender as TCheckBox).checked then
  254.         Options:= Options + [dtvoRowSelect]
  255.      else
  256.         Options:= Options - [dtvoRowSelect]
  257.   end
  258. end;
  259. procedure TDMTreeViewForm.CheckBox3Click(Sender: TObject);
  260. begin
  261.   if (Sender as TCheckBox).checked then
  262.   begin
  263.      with fcDBTreeView1.MultiSelectAttributes do begin
  264.         enabled:= True;
  265.         MultiSelectCheckbox:= true;
  266.      end
  267.   end
  268.   else begin
  269.      with fcDBTreeView1.MultiSelectAttributes do begin
  270.         enabled:= False;
  271.         MultiSelectCheckbox:= False;
  272.      end
  273.   end;
  274. end;
  275. procedure TDMTreeViewForm.FilterComboChange(Sender: TObject);
  276. begin
  277.   with (Sender as TComboBox) do
  278.   begin
  279.      if ItemIndex=0 then CustomersTbl.Filtered:= False
  280.      else if ItemIndex>0 then CustomersTbl.Filtered:= True
  281.   end
  282. end;
  283. procedure TDMTreeViewForm.CustomersTblFilterRecord(DataSet: TDataSet;
  284.   var Accept: Boolean);
  285. begin
  286.    if FilterCombo.ItemIndex=1 then
  287.       Accept:= DataSet.FieldByName('Country').asString = 'US'
  288.    else if FilterCombo.ItemIndex=2 then
  289.       Accept:= not (DataSet.FieldByName('Country').asString = 'US')
  290. end;
  291. procedure TDMTreeViewForm.CheckBox4Click(Sender: TObject);
  292. begin
  293.   with fcDBTreeView1 do begin
  294.      if (Sender as TCheckBox).checked then
  295.         Options:= Options + [dtvoHotTracking]
  296.      else
  297.         Options:= Options - [dtvoHotTracking]
  298.   end
  299. end;
  300. procedure TDMTreeViewForm.CheckBox5Click(Sender: TObject);
  301. begin
  302.   with fcDBTreeView1 do begin
  303.      if (Sender as TCheckBox).checked then
  304.      begin
  305.         Imager:= fcImager1;
  306.         LineColor:= clWhite;
  307.         Font.color:= clWhite;
  308.         Font.style:= Font.Style + [fsBold];
  309.         InactiveFocusColor:= clGray;
  310.      end
  311.      else begin
  312.         Imager:=nil;
  313.         LineColor:= clBtnShadow;
  314.         Font.color:= clBlack;
  315.         Font.style:= Font.Style - [fsBold];
  316.         InactiveFocusColor:= clBtnFace;
  317.      end
  318.   end
  319. end;
  320. procedure TDMTreeViewForm.fcDBTreeView1DrawText(
  321.   TreeView: TfcDBCustomTreeView; Node: TfcDBTreeNode; ARect: TRect;
  322.   var DefaultDrawing: Boolean);
  323. begin
  324. { Set hot-track color to yellow if imager in background }
  325.   if Node.Hot then
  326.   begin
  327.      if (TreeView.Imager<>nil) then
  328.         TreeView.Canvas.Font.Color:= clYellow
  329.      else begin
  330.         if Node.MultiSelected then
  331.            TreeView.Canvas.Font.Color:= clWhite
  332.      end
  333.   end
  334. end;
  335. end.