Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project TWOVIEWS

Project Structure


TWOVIEWS.DPR

program TwoViews;

uses
  Forms,
  GridView in 'GridView.pas' {Form1},
  DataM in 'DataM.pas' {DataModule2},
  FormView in 'FormView.pas' {Form3},
  RangeDb in 'RangeDb.pas' {FormRange};

{$R *.RES}

begin
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TDataModule2, DataModule2);
  Application.CreateForm(TForm3, Form3);
  Application.CreateForm(TFormRange, FormRange);
  Application.Run;
end.

GRIDVIEW.PAS

unit GridView;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    DBNavigator1: TDBNavigator;
    SpeedButtonView: TSpeedButton;
    DBGrid1: TDBGrid;
    RangeSpeedButton: TSpeedButton;
    Panel2: TPanel;
    procedure SpeedButtonViewClick(Sender: TObject);
    procedure RangeSpeedButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses DataM, FormView;

{$R *.DFM}

procedure TForm1.SpeedButtonViewClick(Sender: TObject);
begin
  Form3.Show;
end;

procedure TForm1.RangeSpeedButtonClick(Sender: TObject);
begin
  DataModule2.ChooseRange;
end;

end.

DATAM.PAS

unit DataM;

interface

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

type
  TDataModule2 = 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 Table1NewRecord(DataSet: TDataSet);
    procedure Table1BeforeInsert(DataSet: TDataSet);
    procedure Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
  public
    Max: Integer;
    procedure ComputeMax;
    procedure ChooseRange;
  end;

var
  DataModule2: TDataModule2;

implementation

uses RangeDb;

{$R *.DFM}

procedure TDataModule2.ComputeMax;
var
  Bookmark: TBookmark;
begin
  // save a bookmark
  Bookmark := Table1.GetBookmark;
  try
    Table1.DisableControls;
    Max := 0;
    try
      Table1.First;
      while not Table1.EOF do
      begin
        if Table1CustNo.AsInteger > Max then
          Max := Table1CustNo.AsInteger;
        Table1.Next;
      end;
    finally
      Table1.EnableControls;
    end;
  finally
    // return to the bookmark
    Table1.GotoBookmark (Bookmark);
    Table1.FreeBookmark (Bookmark);
  end;
end;

procedure TDataModule2.Table1NewRecord(DataSet: TDataSet);
begin
  Table1CustNo.Value := Max + 1;
end;

procedure TDataModule2.Table1BeforeInsert(DataSet: TDataSet);
begin
  ComputeMax;
end;

procedure TDataModule2.ChooseRange;
begin
  FormRange.Show;
end;

procedure TDataModule2.Table1FilterRecord(
  DataSet: TDataSet;
  var Accept: Boolean);
begin
{  if (Table1Country.Value = 'US') or
      (Table1Country.Value = 'US Virgin Islands') or
      (Table1State.Value = 'Jamaica') then
    Accept := True
  else
    Accept := False; }

  {if the item corresponding to the country in the
  listbox is active, then view record}
  with FormRange.ListBoxCountries do
    Accept := Selected [Items.IndexOf (Table1Country.AsString)];
  with FormRange.ListBoxStates do
    if Selected [Items.IndexOf (Table1State.AsString)] then
      Accept := True;
end;

end.

FORMVIEW.PAS

unit FormView;

interface

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

type
  TForm3 = 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;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses DataM;

{$R *.DFM}

end.

RANGEDB.PAS

unit RangeDb;

interface

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

type
  TFormRange = class(TForm)
    CheckBoxRange: TCheckBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Bevel1: TBevel;
    Bevel2: TBevel;
    ListBoxStates: TListBox;
    ListBoxCountries: TListBox;
    Label3: TLabel;
    Label4: TLabel;
    CheckBoxFiltering: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormRange: TFormRange;

implementation

uses DataM;

{$R *.DFM}

procedure TFormRange.FormCreate(Sender: TObject);
begin
  with DataModule2 do
  begin
    Table1.First;
    while not Table1.EOF do
    begin
      // add unique values
      if not Table1Country.IsNull and
        (ListBoxCountries.Items.IndexOf (
          Table1Country.AsString) < 0) then
        ListBoxCountries.Items.Add (Table1Country.AsString);
      if not Table1State.IsNull and
        (ListBoxStates.Items.IndexOf (
          Table1State.AsString) < 0) then
        ListBoxStates.Items.Add (Table1State.AsString);
      Table1.Next;
    end;
    // reset the table
    Table1.First;
  end;
end;

procedure TFormRange.BitBtn1Click(Sender: TObject);
begin
  with DataModule2.Table1 do
  begin
    if CheckBoxRange.Checked then
      SetRange ([Edit1.Text], [Edit2.Text])
    else
      CancelRange;
    Filtered := CheckBoxFiltering.Checked;
    Refresh;
  end;
end;

procedure TFormRange.BitBtn2Click(Sender: TObject);
begin
  Close;
end;

end.

GRIDVIEW.DFM

object Form1: TForm1
  Left = 247
  Top = 114
  Width = 489
  Height = 300
  Caption = 'Grid View'
  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 Panel1: TPanel
    Left = 0
    Top = 0
    Width = 481
    Height = 41
    Align = alTop
    ParentShowHint = False
    ShowHint = True
    TabOrder = 0
    object SpeedButtonView: TSpeedButton
      Left = 16
      Top = 8
      Width = 25
      Height = 25
      Hint = 'Open Form View'
      Flat = True
      Glyph.Data = {
        76010000424D7601000000000000760000002800000020000000100000000100
        04000000000000010000120B0000120B00001000000000000000000000000000
        800000800000008080008000000080008000808000007F7F7F00BFBFBF000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        33333FFFFFFFFFFFFFFF000000000000000077777777777777770FFFFFFFFFFF
        FFF07F3FF3FF3FFF3FF70F00F00F000F00F07F773773777377370FFFFFFFFFFF
        FFF07F3FF3FF33FFFFF70F00F00FF00000F07F773773377777F70FEEEEEFF0F9
        FCF07F33333337F7F7F70FFFFFFFF0F9FCF07F3FFFF337F737F70F0000FFF0FF
        FCF07F7777F337F337370F0000FFF0FFFFF07F777733373333370FFFFFFFFFFF
        FFF07FFFFFFFFFFFFFF70CCCCCCCCCCCCCC07777777777777777088CCCCCCCCC
        C880733777777777733700000000000000007777777777777777333333333333
        3333333333333333333333333333333333333333333333333333}
      NumGlyphs = 2
      ParentShowHint = False
      ShowHint = True
      OnClick = SpeedButtonViewClick
    end
    object RangeSpeedButton: TSpeedButton
      Left = 281
      Top = 8
      Width = 57
      Height = 25
      Caption = 'Range...'
      Flat = True
      OnClick = RangeSpeedButtonClick
    end
    object DBNavigator1: TDBNavigator
      Left = 41
      Top = 8
      Width = 240
      Height = 25
      DataSource = DataModule2.DataSource1
      Flat = True
      TabOrder = 0
    end
  end
  object DBGrid1: TDBGrid
    Left = 0
    Top = 41
    Width = 448
    Height = 232
    Align = alClient
    DataSource = DataModule2.DataSource1
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Panel2: TPanel
    Left = 448
    Top = 41
    Width = 33
    Height = 232
    Align = alRight
    AutoSize = True
    BorderWidth = 1
    Caption = 'Panel2'
    UseDockManager = False
    DockSite = True
    TabOrder = 2
  end
end

DATAM.DFM

object DataModule2: TDataModule2
  OldCreateOrder = True
  Left = 27
  Top = 63
  Height = 612
  Width = 812
  object Table1: TTable
    Active = True
    BeforeInsert = Table1BeforeInsert
    OnNewRecord = Table1NewRecord
    DatabaseName = 'DBDEMOS'
    OnFilterRecord = Table1FilterRecord
    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
    Left = 88
    Top = 16
  end
end

FORMVIEW.DFM

object Form3: TForm3
  Left = 514
  Top = 124
  Width = 289
  Height = 360
  Caption = 'Form View'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Visible = True
  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 = DataModule2.DataSource1
    TabOrder = 0
  end
  object DBEdit2: TDBEdit
    Left = 88
    Top = 64
    Width = 177
    Height = 21
    DataField = 'Company'
    DataSource = DataModule2.DataSource1
    TabOrder = 1
  end
  object DBEdit3: TDBEdit
    Left = 88
    Top = 88
    Width = 177
    Height = 21
    DataField = 'Addr1'
    DataSource = DataModule2.DataSource1
    TabOrder = 2
  end
  object DBEdit4: TDBEdit
    Left = 88
    Top = 112
    Width = 177
    Height = 21
    DataField = 'Addr2'
    DataSource = DataModule2.DataSource1
    TabOrder = 3
  end
  object DBEdit5: TDBEdit
    Left = 88
    Top = 136
    Width = 97
    Height = 21
    DataField = 'City'
    DataSource = DataModule2.DataSource1
    TabOrder = 4
  end
  object DBEdit6: TDBEdit
    Left = 88
    Top = 160
    Width = 97
    Height = 21
    DataField = 'State'
    DataSource = DataModule2.DataSource1
    TabOrder = 5
  end
  object DBEdit7: TDBEdit
    Left = 88
    Top = 184
    Width = 97
    Height = 21
    DataField = 'Zip'
    DataSource = DataModule2.DataSource1
    TabOrder = 6
  end
  object DBEdit8: TDBEdit
    Left = 88
    Top = 208
    Width = 97
    Height = 21
    DataField = 'Country'
    DataSource = DataModule2.DataSource1
    TabOrder = 7
  end
  object DBEdit9: TDBEdit
    Left = 88
    Top = 232
    Width = 97
    Height = 21
    DataField = 'Phone'
    DataSource = DataModule2.DataSource1
    MaxLength = 12
    TabOrder = 8
  end
  object DBEdit10: TDBEdit
    Left = 88
    Top = 256
    Width = 97
    Height = 21
    DataField = 'FAX'
    DataSource = DataModule2.DataSource1
    MaxLength = 12
    TabOrder = 9
  end
  object DBEdit11: TDBEdit
    Left = 88
    Top = 280
    Width = 97
    Height = 21
    DataField = 'TaxRate'
    DataSource = DataModule2.DataSource1
    TabOrder = 10
  end
  object DBEdit12: TDBEdit
    Left = 88
    Top = 304
    Width = 177
    Height = 21
    DataField = 'Contact'
    DataSource = DataModule2.DataSource1
    TabOrder = 11
  end
  object DBNavigator1: TDBNavigator
    Left = 16
    Top = 8
    Width = 250
    Height = 25
    DataSource = DataModule2.DataSource1
    TabOrder = 12
  end
end

RANGEDB.DFM

object FormRange: TFormRange
  Left = 468
  Top = 253
  BorderStyle = bsDialog
  Caption = 'Choose Range'
  ClientHeight = 283
  ClientWidth = 292
  Color = clBtnFace
  Constraints.MinHeight = 300
  Constraints.MinWidth = 300
  DragKind = dkDock
  DragMode = dmAutomatic
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 17
    Top = 45
    Width = 19
    Height = 13
    Caption = 'First'
  end
  object Label2: TLabel
    Left = 17
    Top = 71
    Width = 20
    Height = 13
    Caption = 'Last'
  end
  object Bevel1: TBevel
    Left = 8
    Top = 8
    Width = 193
    Height = 97
  end
  object Bevel2: TBevel
    Left = 8
    Top = 112
    Width = 281
    Height = 169
  end
  object Label3: TLabel
    Left = 16
    Top = 144
    Width = 55
    Height = 13
    Caption = 'Filter States'
  end
  object Label4: TLabel
    Left = 152
    Top = 144
    Width = 69
    Height = 13
    Caption = 'Filter Countries'
  end
  object CheckBoxRange: TCheckBox
    Left = 45
    Top = 16
    Width = 97
    Height = 17
    Caption = 'Range Active'
    TabOrder = 0
  end
  object Edit1: TEdit
    Left = 56
    Top = 41
    Width = 129
    Height = 21
    TabOrder = 1
    Text = 'Abacus'
  end
  object Edit2: TEdit
    Left = 56
    Top = 67
    Width = 129
    Height = 21
    TabOrder = 2
    Text = 'Custom'
  end
  object BitBtn1: TBitBtn
    Left = 208
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Apply'
    TabOrder = 3
    OnClick = BitBtn1Click
    Kind = bkOK
  end
  object BitBtn2: TBitBtn
    Left = 208
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Close'
    TabOrder = 4
    OnClick = BitBtn2Click
    Kind = bkCancel
  end
  object ListBoxStates: TListBox
    Left = 16
    Top = 160
    Width = 129
    Height = 113
    ExtendedSelect = False
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 5
  end
  object ListBoxCountries: TListBox
    Left = 152
    Top = 160
    Width = 129
    Height = 113
    ExtendedSelect = False
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 6
  end
  object CheckBoxFiltering: TCheckBox
    Left = 48
    Top = 120
    Width = 97
    Height = 17
    Caption = 'Filtering Active'
    TabOrder = 7
  end
end