Marco Cantù 1998, Mastering Delphi 4

Project: FMTTEST.DPR


Project Structure


FMTTEST.DPR

program FmtTest;

uses
  Forms,
  FtmTestF in 'FtmTestF.pas' {FormFmtTest};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TFormFmtTest, FormFmtTest);
  Application.Run;
end.

FTMTESTF.PAS

unit FtmTestF;

interface

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

type
  TFormFmtTest = class(TForm)
    EditFmtInt: TEdit;
    EditFmtFloat: TEdit;
    BtnFloat: TButton;
    ListBoxInt: TListBox;
    ListBoxFloat: TListBox;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Label1: TLabel;
    Label2: TLabel;
    BtnInt: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    EditInt: TEdit;
    EditFloat: TEdit;
    Label6: TLabel;
    BtnPointer: TButton;
    procedure BtnIntClick(Sender: TObject);
    procedure BtnFloatClick(Sender: TObject);
    procedure BtnPointerClick(Sender: TObject);
    procedure ListBoxIntClick(Sender: TObject);
    procedure ListBoxFloatClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormFmtTest: TFormFmtTest;

implementation

{$R *.DFM}

procedure TFormFmtTest.BtnIntClick(Sender: TObject);
begin
  ShowMessage (Format (EditFmtInt.Text,
    [StrToInt (EditInt.Text)]));
  // if the item is not there, add it
  if ListBoxInt.Items.IndexOf (EditFmtInt.Text) < 0 then
    ListBoxInt.Items.Add (EditFmtInt.Text);
end;

procedure TFormFmtTest.BtnFloatClick(Sender: TObject);
begin
  ShowMessage (Format (EditFmtFloat.Text,
    [StrToFloat (EditFloat.Text)]));
  // if the item is not there, add it
  if ListBoxFloat.Items.IndexOf (EditFmtFloat.Text) < 0 then
    ListBoxFloat.Items.Add (EditFmtFloat.Text);
end;

procedure TFormFmtTest.BtnPointerClick(Sender: TObject);
begin
  ShowMessage (Format ('Pointer: %p',
    [Pointer (StrToInt (EditInt.Text))]));
end;

procedure TFormFmtTest.ListBoxIntClick(Sender: TObject);
begin
  EditFmtInt.Text := ListBoxInt.Items [
    ListBoxInt.ItemIndex];
end;

procedure TFormFmtTest.ListBoxFloatClick(Sender: TObject);
begin
  EditFmtFloat.Text := ListBoxFloat.Items [
    ListBoxFloat.ItemIndex];
end;

procedure TFormFmtTest.FormCreate(Sender: TObject);
begin
  {the formatting of the initial value of the float
  edit box should depend on the international settings,
  for this reason we can use the result of a function
  which applies those settings}
  EditFloat.Text := FloatToStr(1234234.12);
end;

end.

FTMTESTF.DFM

object FormFmtTest: TFormFmtTest
  Left = 193
  Top = 108
  Width = 385
  Height = 346
  Caption = 'Format Test'
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Bevel1: TBevel
    Left = 8
    Top = 8
    Width = 177
    Height = 305
  end
  object Bevel2: TBevel
    Left = 192
    Top = 8
    Width = 177
    Height = 305
  end
  object Label1: TLabel
    Left = 16
    Top = 16
    Width = 62
    Height = 13
    Caption = 'Integer value'
  end
  object Label2: TLabel
    Left = 200
    Top = 16
    Width = 92
    Height = 13
    Caption = 'Floating point value'
  end
  object Label3: TLabel
    Left = 16
    Top = 107
    Width = 63
    Height = 13
    Caption = 'Format string:'
  end
  object Label4: TLabel
    Left = 202
    Top = 107
    Width = 63
    Height = 13
    Caption = 'Format string:'
  end
  object Label5: TLabel
    Left = 16
    Top = 152
    Width = 128
    Height = 13
    Caption = 'Ready-to-use format strings'
  end
  object Label6: TLabel
    Left = 200
    Top = 152
    Width = 128
    Height = 13
    Caption = 'Ready-to-use format strings'
  end
  object EditFmtInt: TEdit
    Left = 16
    Top = 123
    Width = 161
    Height = 21
    TabOrder = 0
    Text = '%d'
  end
  object EditFmtFloat: TEdit
    Left = 200
    Top = 123
    Width = 161
    Height = 21
    TabOrder = 1
    Text = '%g'
  end
  object BtnFloat: TButton
    Left = 200
    Top = 72
    Width = 161
    Height = 25
    Caption = 'Show'
    TabOrder = 2
    OnClick = BtnFloatClick
  end
  object ListBoxInt: TListBox
    Left = 16
    Top = 168
    Width = 161
    Height = 105
    ItemHeight = 13
    Items.Strings = (
      'Weight is %d pounds'
      'Weight is %10d pounds'
      'Weight is %-5d pounds'
      'Hex value: %x'
      'Hex value: %8x')
    TabOrder = 3
    OnClick = ListBoxIntClick
  end
  object ListBoxFloat: TListBox
    Left = 200
    Top = 168
    Width = 161
    Height = 137
    ItemHeight = 13
    Items.Strings = (
      'Number: %g'
      'Number: %f'
      'Number: %e'
      'Number: %n'
      'Number: %m'
      'Cents: %12.2n'
      'Value: %3.8f ')
    TabOrder = 4
    OnClick = ListBoxFloatClick
  end
  object BtnInt: TButton
    Left = 16
    Top = 72
    Width = 161
    Height = 25
    Caption = 'Show'
    TabOrder = 5
    OnClick = BtnIntClick
  end
  object EditInt: TEdit
    Left = 16
    Top = 40
    Width = 161
    Height = 21
    TabOrder = 6
    Text = '100'
  end
  object EditFloat: TEdit
    Left = 200
    Top = 40
    Width = 161
    Height = 21
    TabOrder = 7
  end
  object BtnPointer: TButton
    Left = 16
    Top = 280
    Width = 161
    Height = 25
    Caption = 'Show as Pointer (''%p'')'
          TabOrder = 8
    OnClick = BtnPointerClick
  end
end


Copyright Marco Cantù 1998