Marco Cantù 1998, Mastering Delphi 4

Project: NONAWARE.DPR


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;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    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;
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure Table1BeforePost(DataSet: TDataSet);
    procedure Table1AfterInsert(DataSet: TDataSet);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure KeyPress(Sender: TObject; var Key: Char);
    procedure ComboContinentDropDown(Sender: TObject);
    procedure DataSource1StateChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure EditNameExit(Sender: TObject);
    procedure EditCapitalExit(Sender: TObject);
    procedure ComboContinentExit(Sender: TObject);
    procedure EditPopulationExit(Sender: TObject);
    procedure EditAreaExit(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.Button1Click(Sender: TObject);
begin
  Table1.Next;
end;

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

procedure TForm1.KeyPress(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.Button3Click(Sender: TObject);
begin
  Table1.Insert;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Table1.Delete;
end;

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

procedure TForm1.EditNameExit(Sender: TObject);
begin
  if (Table1.State = dsEdit) or (Table1.State = 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 = dsEdit) or (Table1.State = dsInsert) then
    Table1Capital.AsString := EditCapital.Text;
end;

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

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

procedure TForm1.EditAreaExit(Sender: TObject);
begin
  if (Table1.State = dsEdit) or (Table1.State = dsInsert) then
    Table1Area.AsString := EditArea.Text;
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
    TabOrder = 0
    Text = 'EditName'
    OnExit = EditNameExit
    OnKeyPress = KeyPress
  end
  object EditCapital: TEdit
    Left = 128
    Top = 87
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'EditCapital'
    OnExit = EditCapitalExit
    OnKeyPress = KeyPress
  end
  object EditPopulation: TEdit
    Left = 128
    Top = 157
    Width = 121
    Height = 21
    TabOrder = 2
    Text = 'EditPopulation'
    OnExit = EditPopulationExit
    OnKeyPress = KeyPress
  end
  object EditArea: TEdit
    Left = 128
    Top = 192
    Width = 121
    Height = 21
    TabOrder = 3
    Text = 'EditArea'
    OnExit = EditAreaExit
    OnKeyPress = KeyPress
  end
  object ComboContinent: TComboBox
    Left = 128
    Top = 122
    Width = 121
    Height = 21
    ItemHeight = 13
    Items.Strings = (
      'South America'
      'North America'
      'Europe'
      'Asia'
      'Africa')
    TabOrder = 4
    Text = 'ComboContinent'
    OnDropDown = ComboContinentDropDown
    OnExit = ComboContinentExit
    OnKeyPress = KeyPress
  end
  object Button1: TButton
    Left = 320
    Top = 56
    Width = 75
    Height = 25
    Caption = 'N&ext'
    TabOrder = 5
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 320
    Top = 88
    Width = 75
    Height = 25
    Caption = 'P&rior'
    TabOrder = 6
    OnClick = Button2Click
  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
    Caption = '&Insert'
    TabOrder = 8
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 320
    Top = 184
    Width = 75
    Height = 25
    Caption = '&Delete'
    TabOrder = 9
    OnClick = Button4Click
  end
  object Button5: TButton
    Left = 320
    Top = 152
    Width = 75
    Height = 25
    Caption = 'Cance&l'
    TabOrder = 10
    OnClick = Button5Click
  end
  object DataSource1: TDataSource
    DataSet = Table1
    OnStateChange = DataSource1StateChange
    OnDataChange = DataSource1DataChange
    Left = 56
    Top = 8
  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
end


Copyright Marco Cantù 1998