Crash Course Delphi, part 6:
Foot2Meter with RadioButtons

In this version of Foot2Meter, we'll replace the 2 buttons with RadioButtons and 1 button.

We'll also introduce subroutines, which constitute the power of every real programming language.

For the source code, we refer to the code that you downloaded for part 1.

A conditional statement: IF... THEN... ELSE...

What is the first decision to be made by the program?

  • If the first radiobutton is selected, then both of the edit boxes for input must be visible. Also both of the labels above the edits must be visible, the first one should show 'foot' and the second one should show 'inch'.
    Later on, we must convert from foot and inch to meter.

  • If the second radiobutton is selected, then the second edit box must be invisible. Only the first label should be visible and it should show 'meter'.
    Later on, we must convert from meter to foot and inch.

Such a decision is made with an if-then-else statement:

if condition then statement1 else statement2;

The keyword then also may be followed by a block of statements instead of just one statement. The same goes for the else part.

Taking into account that the first radiobutton is button number 0, we replace the line

Foot := StrToFloat(edInput1.Text);

with the following construction:

if RadioGroup.ItemIndex = 0 then
  Foot := StrToFloat(edInput1.Text)
  Meter := StrToFloat(edInput1.Text);

  Important: no semicolon is allowed after an instruction that precedes an else.

Using if... then... else... will improve your code, making it more compact and readable.


Without subroutines, we would have to repeat a lot of code. Subroutines also make your code more readable, easier to debug and easier to update.

ShowFromFootOrMeter is the name of the subroutine that will show the appropriate labels and captions, depending on the radiobutton that is selected.

We have to declare that a procedure named ShowFromFootOrMeter will be used in the program. That declaration appears in the first part of the code, in the interface section:

  { Private declarations }
  procedure ShowFromFootOrMeter;

The code for this procedure is in the implementation section:

procedure TForm1.ShowFromFootOrMeter;
  if RadioGroup1.ItemIndex = 0 then begin
    // Show the components for inches
    lblInput2.Visible := True;
    edInput2.Visible := True;
    edInput1.Text := '';
    edInput2.Text := '';
    // Show the names of the starting length units
    lblInput1.Caption := 'foot';
    lblInput2.Caption := 'inch';
  else begin
    // Hide the components for inches
    lblInput2.Visible := False;
    edInput2.Visible := False;
    edInput1.Text := '';
    // Show the name of the starting length unit
    lblInput1.Caption := 'meter';

When one of the radio buttons is clicked, we call ShowFromFootOrMeter, as can be seen here:

procedure TForm1.RadioGroup1Click(Sender: TObject);
  lblResult.Caption := 'result...';

Also at the start of the program, we call ShowFromFootOrMeter, in the OnCreate event of the form.


Lesson 5 LESSON 5