Marco Web Center |
|
Chapter 13 - Project FldText |
Project Structure |
FldText.dpr |
program FldText; uses Forms, FldTextF in 'FldTextF.pas' {DbaForm}; {$R *.RES} begin Application.Initialize; Application.CreateForm(TDbaForm, DbaForm); Application.Run; end. |
FldTextF.pas |
unit FldTextF; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, StdCtrls, Mask, DBCtrls, ExtCtrls, Grids, DBGrids, ComCtrls; type TDbaForm = class(TForm) Table1: TTable; DataSource1: TDataSource; Panel1: TPanel; Button2: TButton; PageControl1: TPageControl; TabSheet1: TTabSheet; TabSheet2: TTabSheet; DBGrid1: TDBGrid; DBNavigator1: TDBNavigator; DBCheckBox1: TDBCheckBox; DBEdit1: TDBEdit; DBEdit2: TDBEdit; DBComboBox1: TDBComboBox; DBText1: TDBText; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; DBListBox1: TDBListBox; Table1LastName: TStringField; Table1FirstName: TStringField; Table1Department: TSmallintField; Table1Branch: TStringField; Table1Senior: TBooleanField; Table1HireDate: TDateField; procedure FormCreate(Sender: TObject); procedure Table1DepartmentGetText(Sender: TField; var Text: String; DisplayText: Boolean); procedure Table1DepartmentSetText(Sender: TField; const Text: String); private { Private declarations } end; var DbaForm: TDbaForm; implementation {$R *.DFM} procedure TDbaForm.FormCreate(Sender: TObject); begin if not Table1.Exists then begin Table1.CreateTable; ShowMessage ('You can add data to this table'#13 + 'by using the DbAware example of Chapter 11'); end; Table1.Open; end; procedure TDbaForm.Table1DepartmentGetText(Sender: TField; var Text: String; DisplayText: Boolean); begin case Sender.AsInteger of 1: Text := 'Sales'; 2: Text := 'Accounting'; 3: Text := 'Production'; 4: Text := 'Management'; else Text := '[Error]'; end; end; procedure TDbaForm.Table1DepartmentSetText(Sender: TField; const Text: String); begin if Text = 'Sales' then Sender.Value := 1 else if Text = 'Accounting' then Sender.Value := 2 else if Text = 'Production' then Sender.Value := 3 else if Text = 'Management' then Sender.Value := 4 else raise Exception.Create ('Error in Department field conversion'); end; end. |
FldTextF.dfm |
object DbaForm: TDbaForm Left = 196 Top = 109 Width = 489 Height = 289 Caption = 'Workers (Field Text Demo)' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = True OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Panel1: TPanel Left = 0 Top = 0 Width = 481 Height = 41 Align = alTop TabOrder = 0 object Button2: TButton Left = 16 Top = 8 Width = 121 Height = 25 Caption = '&Add Random Data' TabOrder = 0 end end object PageControl1: TPageControl Left = 0 Top = 41 Width = 481 Height = 221 ActivePage = TabSheet1 Align = alClient TabOrder = 1 object TabSheet1: TTabSheet Caption = 'Record View' object DBText1: TDBText Left = 336 Top = 16 Width = 42 Height = 13 AutoSize = True DataField = 'HireDate' DataSource = DataSource1 end object Label1: TLabel Left = 280 Top = 16 Width = 46 Height = 13 Caption = 'Hire date:' end object Label2: TLabel Left = 18 Top = 52 Width = 51 Height = 13 Caption = '&Last Name' FocusControl = DBEdit1 end object Label3: TLabel Left = 18 Top = 80 Width = 50 Height = 13 Caption = '&First Name' FocusControl = DBEdit2 end object Label4: TLabel Left = 18 Top = 112 Width = 34 Height = 13 Caption = '&Branch' FocusControl = DBComboBox1 end object DBNavigator1: TDBNavigator Left = 16 Top = 8 Width = 240 Height = 25 DataSource = DataSource1 TabOrder = 0 end object DBCheckBox1: TDBCheckBox Left = 80 Top = 144 Width = 81 Height = 17 Caption = '&Senior' DataField = 'Senior' DataSource = DataSource1 TabOrder = 1 ValueChecked = 'True' ValueUnchecked = 'False' end object DBEdit1: TDBEdit Left = 80 Top = 48 Width = 121 Height = 21 DataField = 'LastName' DataSource = DataSource1 TabOrder = 2 end object DBEdit2: TDBEdit Left = 80 Top = 80 Width = 121 Height = 21 DataField = 'FirstName' DataSource = DataSource1 TabOrder = 3 end object DBComboBox1: TDBComboBox Left = 80 Top = 112 Width = 121 Height = 21 DataField = 'Branch' DataSource = DataSource1 ItemHeight = 13 Items.Strings = ( 'Baltimore' 'Berlin' 'Boston' 'Brasilia' 'Cape Town' 'Chicago' 'Dallas' 'Denver' 'Dublin' 'Las Vegas' 'London' 'Los Angeles' 'Louisville' 'Mexico City' 'Miami' 'Minneapolis' 'Moscow' 'New Orleans' 'New York' 'Orlando' 'Rome' 'Salt Lake City' 'San Diego' 'San Francisco' 'San Jose' 'Seattle' 'Singapore' 'Tokio' 'Toronto' 'Vancouver') Sorted = True TabOrder = 4 end object DBListBox1: TDBListBox Left = 280 Top = 48 Width = 177 Height = 113 DataField = 'Department' DataSource = DataSource1 ItemHeight = 13 Items.Strings = ( 'Sales' 'Accounting' 'Production' 'Management') TabOrder = 5 end end object TabSheet2: TTabSheet Caption = 'Grid View' ImageIndex = 1 object DBGrid1: TDBGrid Left = 0 Top = 0 Width = 473 Height = 193 Align = alClient DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] end end end object Table1: TTable DatabaseName = 'DBDEMOS' FieldDefs = < item Name = 'LastName' DataType = ftString Size = 20 end item Name = 'FirstName' DataType = ftString Size = 20 end item Name = 'Department' DataType = ftSmallint end item Name = 'Branch' DataType = ftString Size = 20 end item Name = 'Senior' DataType = ftBoolean end item Name = 'HireDate' DataType = ftDate end> StoreDefs = True TableName = 'Workers' Left = 384 Top = 8 object Table1LastName: TStringField FieldName = 'LastName' end object Table1FirstName: TStringField FieldName = 'FirstName' end object Table1Department: TSmallintField Alignment = taLeftJustify FieldName = 'Department' OnGetText = Table1DepartmentGetText OnSetText = Table1DepartmentSetText end object Table1Branch: TStringField FieldName = 'Branch' end object Table1Senior: TBooleanField FieldName = 'Senior' end object Table1HireDate: TDateField FieldName = 'HireDate' end end object DataSource1: TDataSource DataSet = Table1 Left = 344 Top = 8 end end |