Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 6

Chapter 13 - Project NonAware

Project Structure

NonAware.dpr
program NonAware;

uses
  Forms,
  NonAwF in 'NonAwF.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
NonAwF.pas
unit NonAwF;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBTables, DB, StdCtrls, Grids, DBGrids, ComCtrls, DBActns, ActnList,
  Mask, DBCtrls;

type
  TForm1 = class(TForm)
    Table1: TTable;
    EditName: TEdit;
    Table1Name: TStringField;
    Table1Capital: TStringField;
    Table1Continent: TStringField;
    Table1Area: TFloatField;
    Table1Population: TFloatField;
    EditCapital: TEdit;
    EditPopulation: TEdit;
    EditArea: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    ComboContinent: TComboBox;
    Button1: TButton;
    Button2: TButton;
    StatusBar1: TStatusBar;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ActionList1: TActionList;
    DBEdit1: TDBEdit;
    ActionNext: TAction;
    ActionPrior: TAction;
    ActionInsert: TAction;
    ActionPost: TAction;
    ActionCancel: TAction;
    DataSource1: TDataSource;
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure Table1BeforePost(DataSet: TDataSet);
    procedure Table1AfterInsert(DataSet: TDataSet);
    procedure EditKeyPress(Sender: TObject; var Key: Char);
    procedure ComboContinentDropDown(Sender: TObject);
    procedure DataSource1StateChange(Sender: TObject);
    procedure EditNameExit(Sender: TObject);
    procedure EditCapitalExit(Sender: TObject);
    procedure ComboContinentExit(Sender: TObject);
    procedure EditPopulationExit(Sender: TObject);
    procedure EditAreaExit(Sender: TObject);
    procedure ActionCancelExecute(Sender: TObject);
    procedure ActionPriorExecute(Sender: TObject);
    procedure ActionInsertExecute(Sender: TObject);
    procedure ActionPostExecute(Sender: TObject);
    procedure ActionNextExecute(Sender: TObject);
    procedure ActionNextUpdate(Sender: TObject);
    procedure ActionPriorUpdate(Sender: TObject);
    procedure ActionInsertUpdate(Sender: TObject);
    procedure ActionPostUpdate(Sender: TObject);
    procedure ActionCancelUpdate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  EditName.Text := Table1Name.AsString;
  EditCapital.Text := Table1Capital.AsString;
  ComboContinent.Text := Table1Continent.AsString;
  EditArea.Text := Table1Area.AsString;
  EditPopulation.Text := Table1Population.AsString;
end;

procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table1Area.Value < 100 then
    raise Exception.Create ('Area too small');
end;

procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
begin
  Table1Continent.Value := 'Asia';
 end;

procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Table1.State in [dsEdit, dsInsert]) then
    Table1.Edit;
end;

procedure TForm1.ComboContinentDropDown(Sender: TObject);
begin
  if not (Table1.State in [dsEdit, dsInsert]) then
    Table1.Edit;
end;

procedure TForm1.DataSource1StateChange(Sender: TObject);
var
  strStatus: string;
begin
  case Table1.State of
    dsBrowse: strStatus := 'Browse';
    dsEdit: strStatus := 'Edit';
    dsInsert: strStatus := 'Insert';
  else
    strStatus := 'Other state';
  end;
  StatusBar1.SimpleText := strStatus;
end;

procedure TForm1.EditNameExit(Sender: TObject);
begin
  if Table1.State in [dsEdit, dsInsert] then
    if EditName.Text <> '' then
      Table1Name.AsString := EditName.Text
    else
    begin
      EditName.SetFocus;
      raise Exception.Create ('Undefined Country');
    end;
end;

procedure TForm1.EditCapitalExit(Sender: TObject);
begin
  if Table1.State in [dsEdit, dsInsert] then
    Table1Capital.AsString := EditCapital.Text;
end;

procedure TForm1.ComboContinentExit(Sender: TObject);
begin
  if Table1.State in [dsEdit, dsInsert] then
    Table1Continent.AsString := ComboContinent.Text;
end;

procedure TForm1.EditPopulationExit(Sender: TObject);
begin
  if Table1.State in [dsEdit, dsInsert] then
    Table1Population.AsString := EditPopulation.Text;
end;

procedure TForm1.EditAreaExit(Sender: TObject);
begin
  if Table1.State in [dsEdit, dsInsert] then
    Table1Area.AsString := EditArea.Text;
end;

procedure TForm1.ActionCancelExecute(Sender: TObject);
begin
  Table1.Cancel;
end;

procedure TForm1.ActionPriorExecute(Sender: TObject);
begin
  Table1.Prior
end;

procedure TForm1.ActionInsertExecute(Sender: TObject);
begin
  Table1.Insert;
end;

procedure TForm1.ActionPostExecute(Sender: TObject);
begin
  Table1.Post;
end;

procedure TForm1.ActionNextExecute(Sender: TObject);
begin
  Table1.Next;
end;

procedure TForm1.ActionNextUpdate(Sender: TObject);
begin
  ActionNext.Enabled := not Table1.EOF;
 end;

procedure TForm1.ActionPriorUpdate(Sender: TObject);
begin
  ActionPrior.Enabled := not Table1.BOF;
end;

procedure TForm1.ActionInsertUpdate(Sender: TObject);
begin
  ActionInsert.Enabled := Table1.State = dsBrowse;
end;

procedure TForm1.ActionPostUpdate(Sender: TObject);
begin
  ActionPost.Enabled := Table1.State in [dsEdit, dsInsert];
end;

procedure TForm1.ActionCancelUpdate(Sender: TObject);
begin
  ActionCancel.Enabled := Table1.State in [dsEdit, dsInsert];
end;

end.
NonAwF.dfm
object Form1: TForm1
  Left = 201
  Top = 113
  Width = 470
  Height = 300
  Caption = 'Non Aware'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 40
    Top = 56
    Width = 28
    Height = 13
    Caption = '&Name'
    FocusControl = EditName
  end
  object Label2: TLabel
    Left = 40
    Top = 91
    Width = 32
    Height = 13
    Caption = '&Capital'
    FocusControl = EditCapital
  end
  object Label3: TLabel
    Left = 40
    Top = 126
    Width = 45
    Height = 13
    Caption = 'C&ontinent'
    FocusControl = ComboContinent
  end
  object Label4: TLabel
    Left = 40
    Top = 161
    Width = 50
    Height = 13
    Caption = '&Population'
    FocusControl = EditPopulation
  end
  object Label5: TLabel
    Left = 40
    Top = 196
    Width = 22
    Height = 13
    Caption = '&Area'
    FocusControl = EditArea
  end
  object EditName: TEdit
    Left = 128
    Top = 52
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 0
    Text = 'EditName'
    OnExit = EditNameExit
    OnKeyPress = EditKeyPress
  end
  object EditCapital: TEdit
    Left = 128
    Top = 87
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 1
    Text = 'EditCapital'
    OnExit = EditCapitalExit
    OnKeyPress = EditKeyPress
  end
  object EditPopulation: TEdit
    Left = 128
    Top = 157
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 2
    Text = 'EditPopulation'
    OnExit = EditPopulationExit
    OnKeyPress = EditKeyPress
  end
  object EditArea: TEdit
    Left = 128
    Top = 192
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 3
    Text = 'EditArea'
    OnExit = EditAreaExit
    OnKeyPress = EditKeyPress
  end
  object ComboContinent: TComboBox
    Left = 128
    Top = 122
    Width = 121
    Height = 21
    Color = clWindow
    ItemHeight = 13
    TabOrder = 4
    Text = 'ComboContinent'
    OnDropDown = ComboContinentDropDown
    OnExit = ComboContinentExit
    OnKeyPress = EditKeyPress
    Items.Strings = (
      'South America'
      'North America'
      'Europe'
      'Asia'
      'Africa')
  end
  object Button1: TButton
    Left = 320
    Top = 56
    Width = 75
    Height = 25
    Action = ActionNext
    TabOrder = 5
  end
  object Button2: TButton
    Left = 320
    Top = 88
    Width = 75
    Height = 25
    Action = ActionPrior
    TabOrder = 6
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 254
    Width = 462
    Height = 19
    Panels = <>
    SimplePanel = True
  end
  object Button3: TButton
    Left = 320
    Top = 120
    Width = 75
    Height = 25
    Action = ActionInsert
    TabOrder = 8
  end
  object Button4: TButton
    Left = 320
    Top = 184
    Width = 75
    Height = 25
    Action = ActionPost
    TabOrder = 9
  end
  object Button5: TButton
    Left = 320
    Top = 152
    Width = 75
    Height = 25
    Action = ActionCancel
    TabOrder = 10
  end
  object DBEdit1: TDBEdit
    Left = 161
    Top = 16
    Width = 0
    Height = 21
    Color = clWindow
    DataField = 'Name'
    TabOrder = 11
  end
  object Table1: TTable
    Active = True
    AfterInsert = Table1AfterInsert
    BeforePost = Table1BeforePost
    DatabaseName = 'DBDEMOS'
    TableName = 'COUNTRY.DB'
    Left = 16
    Top = 8
    object Table1Name: TStringField
      DisplayWidth = 17
      FieldName = 'Name'
      Visible = False
      Size = 24
    end
    object Table1Capital: TStringField
      DisplayWidth = 18
      FieldName = 'Capital'
      Size = 24
    end
    object Table1Continent: TStringField
      DisplayWidth = 18
      FieldName = 'Continent'
      Size = 24
    end
    object Table1Area: TFloatField
      DisplayWidth = 12
      FieldName = 'Area'
    end
    object Table1Population: TFloatField
      DisplayWidth = 12
      FieldName = 'Population'
    end
  end
  object ActionList1: TActionList
    Left = 112
    Top = 8
    object ActionNext: TAction
      Category = 'Dataset'
      Caption = 'ActionNext'
      OnExecute = ActionNextExecute
      OnUpdate = ActionNextUpdate
    end
    object ActionPrior: TAction
      Category = 'Dataset'
      Caption = 'ActionPrior'
      OnExecute = ActionPriorExecute
      OnUpdate = ActionPriorUpdate
    end
    object ActionInsert: TAction
      Category = 'Dataset'
      Caption = 'ActionInsert'
      OnExecute = ActionInsertExecute
      OnUpdate = ActionInsertUpdate
    end
    object ActionPost: TAction
      Category = 'Dataset'
      Caption = 'ActionPost'
      OnExecute = ActionPostExecute
      OnUpdate = ActionPostUpdate
    end
    object ActionCancel: TAction
      Category = 'Dataset'
      Caption = 'ActionCancel'
      OnExecute = ActionCancelExecute
      OnUpdate = ActionCancelUpdate
    end
  end
  object DataSource1: TDataSource
    DataSet = Table1
    OnStateChange = DataSource1StateChange
    OnDataChange = DataSource1DataChange
    Left = 56
    Top = 8
  end
end