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 5

Project MDIVIEW

Project Structure


MDIVIEW.DPR

program MdiView;

uses
  Forms,
  GridView in 'GridView.pas' {GridForm},
  DataM in 'DataM.pas' {CustomerDM},
  FormView in 'FormView.pas' {RecordForm},
  Main in 'Main.pas' {FrameForm};

{$R *.RES}

begin
  Application.CreateForm(TFrameForm, FrameForm);
  Application.Run;
end.

GRIDVIEW.PAS

unit GridView;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, Buttons, DBCtrls, DataM, ExtCtrls;

type
  TGridForm = class(TForm)
    DBGrid1: TDBGrid;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    DM: TCustomerDM;
  public
    { Public declarations }
  end;

implementation

{$R *.DFM}

procedure TGridForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TGridForm.FormCreate(Sender: TObject);
begin
  DM := TCustomerDM.Create (Self);
  DBGrid1.DataSource := DM.DataSource1;
end;

end.

DATAM.PAS

unit DataM;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBTables, DB;

type
  TCustomerDM = class(TDataModule)
    Table1: TTable;
    DataSource1: TDataSource;
    Table1CustNo: TFloatField;
    Table1Company: TStringField;
    Table1Addr1: TStringField;
    Table1Addr2: TStringField;
    Table1City: TStringField;
    Table1State: TStringField;
    Table1Zip: TStringField;
    Table1Country: TStringField;
    Table1Phone: TStringField;
    Table1FAX: TStringField;
    Table1TaxRate: TFloatField;
    Table1Contact: TStringField;
    Table1LastInvoiceDate: TDateTimeField;
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
  end;

implementation

{$R *.DFM}

procedure TCustomerDM.DataSource1DataChange(Sender: TObject;
  Field: TField);
begin
  (Owner as TForm).Caption :=
    (Owner as TForm).Hint + ' - ' +
    Table1Company.AsString;
end;

end.

FORMVIEW.PAS

unit FormView;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, DBCtrls, StdCtrls, Mask, DataM;

type
  TRecordForm = class(TForm)
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit6: TDBEdit;
    DBEdit7: TDBEdit;
    DBEdit8: TDBEdit;
    DBEdit9: TDBEdit;
    DBEdit10: TDBEdit;
    DBEdit11: TDBEdit;
    DBEdit12: TDBEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    DBNavigator1: TDBNavigator;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    DM: TCustomerDM;
  public
    { Public declarations }
  end;

implementation

{$R *.DFM}

procedure TRecordForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TRecordForm.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  DM := TCustomerDM.Create (Self);
  // connect the navigator
  DBNavigator1.DataSource := DM.DataSource1;
  // connect all DBEdit controls
  for I := 0 to ControlCount - 1 do
    if Controls [i] is TDBEdit then
      TDBEdit (Controls [I]).DataSource :=
        DM.DataSource1;
end;

end.

MAIN.PAS

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus;

type
  TFrameForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    NewRecordView1: TMenuItem;
    NewGridView1: TMenuItem;
    Close1: TMenuItem;
    N1: TMenuItem;
    Windows1: TMenuItem;
    Tile1: TMenuItem;
    Cascade1: TMenuItem;
    Help1: TMenuItem;
    About1: TMenuItem;
    procedure NewRecordView1Click(Sender: TObject);
    procedure NewGridView1Click(Sender: TObject);
    procedure Tile1Click(Sender: TObject);
    procedure Cascade1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure Close1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrameForm: TFrameForm;

implementation

uses FormView, GridView;

{$R *.DFM}

procedure TFrameForm.NewRecordView1Click(Sender: TObject);
begin
  with TRecordForm.Create (Application) do
    Show;
end;

procedure TFrameForm.NewGridView1Click(Sender: TObject);
begin
  with TGridForm.Create (Application) do
    Show;
end;

procedure TFrameForm.Tile1Click(Sender: TObject);
begin
  Tile;
end;

procedure TFrameForm.Cascade1Click(Sender: TObject);
begin
  Cascade;
end;

procedure TFrameForm.About1Click(Sender: TObject);
begin
  ShowMessage ('MDI datamodule based demo'#13 +
    'from the book "Mastering Delphi 4"');
end;

procedure TFrameForm.Close1Click(Sender: TObject);
begin
  Close;
end;

end.

GRIDVIEW.DFM

object GridForm: TGridForm
  Left = 286
  Top = 234
  Width = 479
  Height = 300
  Hint = 'Grid View'
  Caption = 'Grid View'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsMDIChild
  OldCreateOrder = True
  Position = poDefault
  Visible = True
  OnClose = FormClose
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 0
    Top = 0
    Width = 471
    Height = 273
    Align = alClient
    DataSource = CustomerDM.DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
end

DATAM.DFM

object CustomerDM: TCustomerDM
  OldCreateOrder = True
  Left = 268
  Top = 373
  Height = 159
  Width = 196
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    IndexName = 'ByCompany'
    TableName = 'CUSTOMER.DB'
    Left = 24
    Top = 16
    object Table1CustNo: TFloatField
      Alignment = taLeftJustify
      DisplayWidth = 7
      FieldName = 'CustNo'
      DisplayFormat = 'CN 0000'
      MaxValue = 9999
      MinValue = 1000
    end
    object Table1Company: TStringField
      DisplayWidth = 25
      FieldName = 'Company'
      Size = 30
    end
    object Table1Addr1: TStringField
      DisplayWidth = 21
      FieldName = 'Addr1'
      Size = 30
    end
    object Table1Addr2: TStringField
      DisplayWidth = 28
      FieldName = 'Addr2'
      Size = 30
    end
    object Table1City: TStringField
      DisplayWidth = 15
      FieldName = 'City'
      Size = 15
    end
    object Table1State: TStringField
      DisplayWidth = 20
      FieldName = 'State'
    end
    object Table1Country: TStringField
      DisplayWidth = 20
      FieldName = 'Country'
    end
    object Table1Zip: TStringField
      DisplayWidth = 10
      FieldName = 'Zip'
      Size = 10
      AttributeSet = 'ZipCode'
    end
    object Table1Phone: TStringField
      DisplayWidth = 15
      FieldName = 'Phone'
      EditMask = '!999-000-0000;1;_'
      Size = 15
    end
    object Table1FAX: TStringField
      DisplayWidth = 15
      FieldName = 'FAX'
      EditMask = '!999-000-0000;1;_'
      Size = 15
      AttributeSet = 'USPhone'
    end
    object Table1TaxRate: TFloatField
      DisplayWidth = 10
      FieldName = 'TaxRate'
      DisplayFormat = '0.00%'
      AttributeSet = 'TaxRate'
    end
    object Table1Contact: TStringField
      DisplayWidth = 20
      FieldName = 'Contact'
    end
    object Table1LastInvoiceDate: TDateTimeField
      DisplayWidth = 13
      FieldName = 'LastInvoiceDate'
    end
  end
  object DataSource1: TDataSource
    DataSet = Table1
    OnDataChange = DataSource1DataChange
    Left = 88
    Top = 16
  end
end

FORMVIEW.DFM

object RecordForm: TRecordForm
  Left = 506
  Top = 209
  Hint = 'Form View'
  BorderStyle = bsSingle
  Caption = 'Form View'
  ClientHeight = 333
  ClientWidth = 281
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsMDIChild
  OldCreateOrder = True
  Position = poDefault
  Visible = True
  OnClose = FormClose
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 16
    Top = 44
    Width = 64
    Height = 13
    Caption = 'Customer No.'
  end
  object Label2: TLabel
    Left = 16
    Top = 68
    Width = 44
    Height = 13
    Caption = 'Company'
  end
  object Label3: TLabel
    Left = 16
    Top = 92
    Width = 53
    Height = 13
    Caption = 'Address (1)'
  end
  object Label4: TLabel
    Left = 16
    Top = 116
    Width = 53
    Height = 13
    Caption = 'Address (2)'
  end
  object Label5: TLabel
    Left = 16
    Top = 140
    Width = 17
    Height = 13
    Caption = 'City'
  end
  object Label6: TLabel
    Left = 16
    Top = 164
    Width = 25
    Height = 13
    Caption = 'State'
  end
  object Label7: TLabel
    Left = 16
    Top = 188
    Width = 17
    Height = 13
    Caption = 'ZIP'
  end
  object Label8: TLabel
    Left = 16
    Top = 212
    Width = 36
    Height = 13
    Caption = 'Country'
  end
  object Label9: TLabel
    Left = 16
    Top = 236
    Width = 31
    Height = 13
    Caption = 'Phone'
  end
  object Label10: TLabel
    Left = 16
    Top = 260
    Width = 20
    Height = 13
    Caption = 'FAX'
  end
  object Label11: TLabel
    Left = 16
    Top = 284
    Width = 44
    Height = 13
    Caption = 'Tax Rate'
  end
  object Label12: TLabel
    Left = 16
    Top = 308
    Width = 37
    Height = 13
    Caption = 'Contact'
  end
  object DBEdit1: TDBEdit
    Left = 88
    Top = 40
    Width = 89
    Height = 21
    DataField = 'CustNo'
    DataSource = CustomerDM.DataSource1
    TabOrder = 0
  end
  object DBEdit2: TDBEdit
    Left = 88
    Top = 64
    Width = 177
    Height = 21
    DataField = 'Company'
    DataSource = CustomerDM.DataSource1
    TabOrder = 1
  end
  object DBEdit3: TDBEdit
    Left = 88
    Top = 88
    Width = 177
    Height = 21
    DataField = 'Addr1'
    DataSource = CustomerDM.DataSource1
    TabOrder = 2
  end
  object DBEdit4: TDBEdit
    Left = 88
    Top = 112
    Width = 177
    Height = 21
    DataField = 'Addr2'
    DataSource = CustomerDM.DataSource1
    TabOrder = 3
  end
  object DBEdit5: TDBEdit
    Left = 88
    Top = 136
    Width = 97
    Height = 21
    DataField = 'City'
    DataSource = CustomerDM.DataSource1
    TabOrder = 4
  end
  object DBEdit6: TDBEdit
    Left = 88
    Top = 160
    Width = 97
    Height = 21
    DataField = 'State'
    DataSource = CustomerDM.DataSource1
    TabOrder = 5
  end
  object DBEdit7: TDBEdit
    Left = 88
    Top = 184
    Width = 97
    Height = 21
    DataField = 'Zip'
    DataSource = CustomerDM.DataSource1
    TabOrder = 6
  end
  object DBEdit8: TDBEdit
    Left = 88
    Top = 208
    Width = 97
    Height = 21
    DataField = 'Country'
    DataSource = CustomerDM.DataSource1
    TabOrder = 7
  end
  object DBEdit9: TDBEdit
    Left = 88
    Top = 232
    Width = 97
    Height = 21
    DataField = 'Phone'
    DataSource = CustomerDM.DataSource1
    MaxLength = 12
    TabOrder = 8
  end
  object DBEdit10: TDBEdit
    Left = 88
    Top = 256
    Width = 97
    Height = 21
    DataField = 'FAX'
    DataSource = CustomerDM.DataSource1
    MaxLength = 12
    TabOrder = 9
  end
  object DBEdit11: TDBEdit
    Left = 88
    Top = 280
    Width = 97
    Height = 21
    DataField = 'TaxRate'
    DataSource = CustomerDM.DataSource1
    TabOrder = 10
  end
  object DBEdit12: TDBEdit
    Left = 88
    Top = 304
    Width = 177
    Height = 21
    DataField = 'Contact'
    DataSource = CustomerDM.DataSource1
    TabOrder = 11
  end
  object DBNavigator1: TDBNavigator
    Left = 16
    Top = 8
    Width = 250
    Height = 25
    DataSource = CustomerDM.DataSource1
    TabOrder = 12
  end
end

MAIN.DFM

object FrameForm: TFrameForm
  Left = 227
  Top = 187
  Width = 696
  Height = 480
  Caption = 'Mdi View'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsMDIForm
  Menu = MainMenu1
  OldCreateOrder = False
  WindowMenu = Windows1
  PixelsPerInch = 96
  TextHeight = 13
  object MainMenu1: TMainMenu
    Left = 40
    Top = 24
    object File1: TMenuItem
      Caption = '&File'
      object NewRecordView1: TMenuItem
        Caption = 'New Record View'
        OnClick = NewRecordView1Click
      end
      object NewGridView1: TMenuItem
        Caption = 'New Grid View'
        OnClick = NewGridView1Click
      end
      object N1: TMenuItem
        Caption = '-'
      end
      object Close1: TMenuItem
        Caption = 'Close'
        OnClick = Close1Click
      end
    end
    object Windows1: TMenuItem
      Caption = 'Windows'
      object Tile1: TMenuItem
        Caption = '&Tile'
        OnClick = Tile1Click
      end
      object Cascade1: TMenuItem
        Caption = '&Cascade'
        OnClick = Cascade1Click
      end
    end
    object Help1: TMenuItem
      Caption = '&Help'
      object About1: TMenuItem
        Caption = '&About...'
        OnClick = About1Click
      end
    end
  end
end