Crash Course Delphi, part 4: Event Handlers


Let's code !

 
 
Our Foot-To-Meter Convertor still doesn't do anything useful.

So, let's add some real code to the event handlers.


OnClick Event Handler for btnFootToMeter

In lesson 3 we've put some temporary code in the event handler of btnFootToMeter. But what should this event handler really do?
  1. The text "foot" should be shown in lblStartUnit.
  2. The text of edInput should be converted to a numerical value.
  3. From this number (the value in foot), the value in meter should be calculated.
  4. This number is converted again to 'text', which is shown in lblResult.
  5. Finally, the text ' meter' should added to the caption of lblResult.

We'll keep temporary values and texts in the variables Foot, Meter and ResultString. Variables have to be declared:
   - you indicate that they are variables via the reserved word var
   - you give their name followed by their type.

This is done BEFORE the reserved word begin in the routine. For the moment, don't worry about all the different types of variables. Later on, we shall come back to this in detail.

Now, let's code!

  1. Start Delphi and open the project foot2metersimp.dpr thru' the File menu and the option Open Project...
  2. Replace the experimental text of btnFootToMeter's OnClick event handler with this new text:
     
    procedure TForm1.btnFootToMeterClick(Sender: TObject);
    var
      Foot, Meter: real;
      ResultString: string;
    begin
      lblStartUnit.Caption := 'foot';
      Foot := StrToFloat(edInput.Text);
      Meter := Foot * 0.3048;
      ResultString := FormatFloat('0.00', Meter);
      lblResult.Caption := ResultString + ' meter';
    end;
  3. Compile and test: it works! ...at least, if you didn't make any typing errors ;-)
    Let's stop the application and continue programming.
  4. btnMeterToFoot should do the "reverse" of btnFootToMeter: the text 'meter' in must be shown in lblStartUnit and the value in meter should be converted to foot (divide it by 0.3048)
    The rest is almost identical to the handler we wrote for btnFootToMeter.

    Select btnMeterToFoot, select the OnClick event in the Object Inspector and double-click the empty field next to it. Delphi gives a name to the new event handler: btnMeterToFootClick.
  5. In the Editor, complete this event handler as follows:
     
    procedure TForm1.btnFootToMeterClick(Sender: TObject);
    var
      Foot, Meter: real;
      ResultString: string;
    begin
      lblStartUnit.Caption := 'meter';
      Meter := StrToFloat(edInput.Text);
      Foot := Meter / 0.3048;
      ResultString := FormatFloat('0.00', Foot);
      lblResult.Caption := ResultString + ' foot';
    end;
  6. Compile and test. Congratulations!
  7. Save the project (Save All).

Adding Comments

Adding comments to your source code is of the uttermost importance. At the moment that you're writing the code, you know exactly what you mean by it. But what in a week, a month, a year? Yeah, I've heard the saying that Real Programmers don't Comment their Code... Worse than Real Men don't Read the Manual, because you eventually can read that one, if the dog didn't chew on it ;-)

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

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

{ Text between curly braces is a comment. }

{ Comments between curly braces can extend over several lines.
  Curly braces are sometimes called squiggly braces, and 
  another word for braces is brackets.
  This comments block ends at the end of this line.      }

(* The combination of rounded brackets and asterix may be used
   instead of the curly 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 Code Editor, comments are displayed differently: in green (or in blue, depending from the version of Delphi) and in 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.

Important! There is an exception!
Not everything between curly brackets { } is comments! Lines starting with {$ are compiler directives, they are not comments and should NOT be removed!
For example,  {$R *.DFM} is not a comments, but an indication for the Delphi compiler that it must include the DFM-files (the "form" files) in the final application.

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

unit unit1;

interface

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

type
  TForm1 = class(TForm)
    edInput: TEdit;
    lblStartUnit: TLabel;
    Label2: TLabel;
    lblResult: TLabel;
    btnFootToMeter: TButton;
    btnMeterToFoot: TButton;
    procedure btnFootToMeterClick(Sender: TObject);
    procedure btnMeterToFootClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ Event handler for the OnClick event of btnFootToMeter:
  conversion from foot to meter.
}
procedure TForm1.EuroButtonClick(Sender: TObject);
var
  Foot, Meter: real;
  ResultString: string;
begin
  // Show the name of the starting length unit
  lblStartUnit.Caption := 'foot';

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

  // Convert from foot to meter
 Meter := Foot * 0.3048; { 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' } ResultString := FormatFloat('0.00', Meter);

// Add the string ' meter' ResultString := ResultString + ' meter'; // Put the string in the CAPTION of the LABEL lblResult.Caption := ResultString; end; { Event handler for the OnClick event of btnMeterToFoot: conversion from meter to foot. } procedure TForm1.btnMeterToFootClick(Sender: TObject); var Foot, Meter: real; ResultString: string; begin // Show the name of the starting length unit lblStartUnit.Caption := 'meter'; // Convert the text of the EDIT to a numerical value Meter := StrToFloat(edInput.Text); // Convert from foot to meter
 Foot := Meter / 0.3048; { 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' } ResultString := FormatFloat('0.00', Foot);

// Add the string ' foot' ResultString := ResultString + ' foot'; // Put the string in the CAPTION of the LABEL lblResult.Caption := ResultString; end; end.

Lesson 3 LESSON 3     TOP   HOME     LESSON 5 Lesson 5