Lesson 3: Let's code !

  Our Euro Currency Convertor still doesn't do anything useful. In this lesson, we'll add some real code to the event handlers.

Let's code !

In lesson 2 we've put some temporary code in the event handler of EuroButton. But what should this event handler really do?

We keep all temporary values and texts in the variables Euro, Fr, S1 and S2. Variables have to be declared: indicate that they are variables and give their names followed by their types. This is done BEFORE the word begin in the routine.

  1. Let's code! Start Delphi and open the project euronl01.dpr thru' the File menu and the option Open Project...

  2. Replace the experimental text of EuroButton's OnClick event handler with new text:
    procedure TForm1.EuroButtonClick(Sender: TObject);
    var
      Euro, Fr: real;
      S1, S2: string;
    begin
      CurrLabel.Caption := 'Euro';
      Euro := StrToFloat(InputEdit.Text);
      Fr := Euro * 40.3399;
      S1 := FormatFloat('0.00', Euro);
      S2 := FormatFloat('0.00', Fr);
      EuroLabel.Caption := S1;
      BEFLabel.Caption := S2;
    end;
    

  3. Compile and test: it works! ...at least, if you didn't make any typing errors ;-)

  4. Let's stop the application and continue programming.

    BEFButton should do the "reverse" of EuroButton: the text "BEF" in must be shown in CurrLabel and the value in Euro should be converted to BEF (divide it by 40.3399). The rest is almost identical to the handler we wrote for EuroButton.

    Select BEFButton, select the OnClick event in the Object Inspector and double-click the white field next to it. Delphi gives a name to the new event handler: BEFButtonClick.

    In the Editor, complete this event handler as follows:

    procedure TForm1.BEFButtonClick(Sender: TObject);
    var
      Euro, Fr: real;
      S1, S2: string;
    begin
      CurrLabel.Caption := 'BEF';
      Fr := StrToFloat(InputEdit.Text);
      Euro := Fr / 40.3399;
      S1 := FormatFloat('0.00', Euro);
      S2 := FormatFloat('0.00', Fr);
      EuroLabel.Caption := S1;
      BEFLabel.Caption := S2;
    end;
    

  5. Compile and test. Congratulations!

  6. Save the project (Save All).

Adding Comments

You can add comments (or Remarks) to a Delphi unit in several ways:

// Any text between a double-slash and the end of the line is comments

// Look at this:
S1 := Edit1.Text // the text of Edit1 is assigned to S1

{ Text between a left brace and a right brace is a comment.}

{ Comments between braces can extend over several lines.
  This comments block ends at the end of this line.      }

(* The combination of rounded brackets and asterix may be used
   instead of the brackets { }. This is useful if you want to 
   'nest' comments, which is impossible if you use only ONE 
   type of comments delimiters.
   I never use this notation for comments. I use it to disable
   code temporarily during tests, as in the following example: *)

(*
  { This code seems to be faulty. Let's disable it and see if
    things get better that way.                               }

  Label1.Text := Edit1.Text;
*)

In the Editor, comments are displayed differently: in blue and Italic. On these pages I won't show comments in Italic, that would be a bit overdone and it would be difficult to read.

Also some other elements can be rendered in a "special" way: reserved words in bold, text (strings in Delphi-language) in blue, numbers in red, and so on. The settings for this are in the Tools menu, Environment Options, tab Color.

I added some comments to the unit euroform.pas. Look at the following code and maybe add some comments to your version of the unit:

unit euroform;

interface

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

type
  TForm1 = class(TForm)
    InputEdit: TEdit;
    CurrLabel: TLabel;
    Label2: TLabel;
    EuroLabel: TLabel;
    BEFLabel: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    EuroButton: TButton;
    BEFButton: TButton;
    procedure EuroButtonClick(Sender: TObject);
    procedure BEFButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ Event handler for the OnClick event of EuroButton:
  conversion from Euro to BEF.
  Shows the name of the starting currency, converts the text
  of the Edit-box to Belgian francs, shows the results.      }
procedure TForm1.EuroButtonClick(Sender: TObject);
var
  Euro, Fr: real;
  S1, S2: string;
begin
  // Show the name of the starting currency
  CurrLabel.Caption := 'Euro';

  // Convert the text of the EDIT to a numerical value
  Euro := StrToFloat(InputEdit.Text);

  // Convert from Euro to BEF
  Fr := Euro * 40.3399;

  { Convert the floating point numbers to string-format.
    '0.00' is the 'Format String', meaning:
    'format the result with 2 decimal digits to the right
     of the decimal separator'                            }
  S1 := FormatFloat('0.00', Euro);
  S2 := FormatFloat('0.00', Fr);

  // Put the strings in the CAPTION of the LABELs
  EuroLabel.Caption := S1;
  BEFLabel.Caption := S2;
end;

{ Event handler for the Onclick event of BEFButton:
  conversion from BEF to Euro.
  Shows the name of the starting currency, converts the text
  of the Edit-box to Euro, shows the results.                }
procedure TForm1.BEFButtonClick(Sender: TObject);
var
  Euro, Fr: real;
  S1, S2: string;
begin
  // Show the name of the starting currency
  MuntLabel.Caption := 'BEF';

  // Convert the text of the EDIT to a numerical value
  Fr := StrToFloat(InputEdit.Text);

  // Convert from BEF to Euro
  Euro := Fr / 40.3399;

  { Convert the floating point numbers to string-format.
    '0.00' is the 'Format String', meaning:
    'format the result with 2 decimal digits to the right
     of the decimal separator'                            }
  S1 := FormatFloat('0.00', Euro);
  S2 := FormatFloat('0.00', Fr);

  // Put the strings in the CAPTION of the LABELs
  EuroLabel.Caption := S1;
  BEFLabel.Caption := S2;
end;

end.

See you again in lesson 4!

[ TOP ]

© Copyright 1999 
Studiebureau Festraets