Marco Web Center |
Home: Code Repository: Mastering Delphi 5Project CALC
Project StructureCALC.DPRprogram Calc; uses Forms, CalcF in 'CalcF.pas' {CalcForm}; {$R *.RES} begin Application.CreateForm(TCalcForm, CalcForm); Application.Run; end. CALCF.PASunit CalcF; interface uses SysUtils, Windows, Messages, Classes, Graphics, Controls, StdCtrls, Forms, DBCtrls, DB, DBGrids, DBTables, Grids, ExtCtrls; type TCalcForm = class(TForm) DBGrid1: TDBGrid; DBNavigator: TDBNavigator; Panel1: TPanel; Panel2: TPanel; DataSource1: TDataSource; Table1: TTable; Table1PopulationDensity: TFloatField; Table1Area: TFloatField; Table1Population: TFloatField; Table1Name: TStringField; Table1Capital: TStringField; Table1Continent: TStringField; procedure FormCreate(Sender: TObject); procedure Table1CalcFields(DataSet: TDataset); procedure DBGrid1EditButtonClick(Sender: TObject); private { private declarations } public { public declarations } end; var CalcForm: TCalcForm; implementation uses Dialogs; {$R *.DFM} procedure TCalcForm.FormCreate(Sender: TObject); begin Table1.Open; end; procedure TCalcForm.Table1CalcFields(DataSet: TDataset); begin // plain version (very dangerous) { Table1PopulationDensity.Value := Table1Population.Value / Table1Area.Value;} // version based on exceptions (ok) { try Table1PopulationDensity.Value := Table1Population.Value / Table1Area.Value; except on Exception do Table1PopulationDensity.Value := 0; end;} // definitive version if not Table1Area.IsNull and (Table1Area.Value <> 0) then Table1PopulationDensity.Value := Table1Population.Value / Table1Area.Value else Table1PopulationDensity.Value := 0; end; procedure TCalcForm.DBGrid1EditButtonClick(Sender: TObject); begin MessageDlg (Format ( 'The population density (%.2n)'#13 + 'is the Population (%.0n)'#13 + 'devided by the Area (%.0n).'#13#13 + 'Edit these two fields to change it.', [Table1PopulationDensity.AsFloat, Table1Population.AsFloat, Table1Area.AsFloat]), mtInformation, [mbOK], 0); end; end. CALCF.DFMobject CalcForm: TCalcForm Left = 335 Top = 304 Width = 556 Height = 236 ActiveControl = Panel1 Caption = 'Calculated Field' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clBlack Font.Height = -11 Font.Name = 'Arial' Font.Style = [] OldCreateOrder = True Position = poScreenCenter OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 14 object Panel1: TPanel Left = 0 Top = 0 Width = 548 Height = 33 Align = alTop TabOrder = 0 object Panel2: TPanel Left = 406 Top = 1 Width = 141 Height = 31 Align = alRight BevelOuter = bvNone Caption = 'Panel2' TabOrder = 0 object DBNavigator: TDBNavigator Left = 18 Top = 6 Width = 116 Height = 19 DataSource = DataSource1 VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast] TabOrder = 0 end end end object DBGrid1: TDBGrid Left = 0 Top = 33 Width = 548 Height = 176 Align = alClient DataSource = DataSource1 TabOrder = 1 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clBlack TitleFont.Height = -11 TitleFont.Name = 'Arial' TitleFont.Style = [] OnEditButtonClick = DBGrid1EditButtonClick Columns = < item Expanded = False FieldName = 'Name' Visible = True end item Expanded = False FieldName = 'Capital' Visible = True end item DropDownRows = 3 Expanded = False FieldName = 'Continent' PickList.Strings = ( 'North America' 'South America') Width = 87 Visible = True end item Expanded = False FieldName = 'Population' Visible = True end item Expanded = False FieldName = 'Area' Visible = True end item ButtonStyle = cbsEllipsis Expanded = False FieldName = 'Population Density' ReadOnly = True Visible = True end> end object DataSource1: TDataSource DataSet = Table1 Left = 77 Top = 65533 end object Table1: TTable Active = True OnCalcFields = Table1CalcFields DatabaseName = 'DBDEMOS' TableName = 'country.db' Left = 10 Top = 65533 object Table1Name: TStringField DisplayLabel = 'Country' DisplayWidth = 11 FieldName = 'Name' FixedChar = False Size = 24 end object Table1Capital: TStringField DisplayWidth = 13 FieldName = 'Capital' FixedChar = False Size = 24 end object Table1Continent: TStringField FieldName = 'Continent' FixedChar = False Size = 24 end object Table1Population: TFloatField DisplayWidth = 14 FieldName = 'Population' DisplayFormat = '###,###,###' end object Table1Area: TFloatField DisplayWidth = 13 FieldName = 'Area' DisplayFormat = '###,###,###' end object Table1PopulationDensity: TFloatField DisplayWidth = 16 FieldKind = fkCalculated FieldName = 'Population Density' DisplayFormat = '###.##' Precision = 2 Calculated = True end end end
|
||
© Copyright Marco Cantù, 1995-2020, All rights reserved |