Crash Course Delphi, part 4: Event Handlers |
Let's code !
OnClick Event Handler for btnFootToMeterIn lesson 3 we've put some temporary code in the event handler of btnFootToMeter. But what should this event handler really do?
We'll keep temporary values and texts in the variables Foot, Meter and ResultString.
Variables have to be declared: 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!
|
![]() Table of contents » Compiling a project » Form, Edits, Buttons » Properties and Events » Event Handlers » Validating the inputs » With Radio Buttons |
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 |