Writing your First Form-based Application using Delphi 7

Before doing this exercise you must first have Delphi installed on your PC.

The steps to output "Hello World!" on a form are as follows:

  1. Start Delphi by double-clicking on its Launch icon.
    A new application starts by default.
  2. Save all files. Shift+Ctrl+S gives the Save Unit1 As dialogue followed by the Save Project1 As dialogue. Navigate to a suitable folder and save the unit as uHelloWorld, to which Delphi will be give the .pas extension. Save the project as HelloWorld to which Delphi 7 will give the .dpr extension.
  3. Open the View forms dialogue (Shift+F12) then select Form1 and click on the OK button.
  4. Reduce the size of the form by dragging its bottom right corner towards the top left corner. Leave room for a label.
  5. Click on the A button then on the form to add a label component to the form.

    Label icon, tooltip and form

    The label icon is shown here with its tooltip. (Make sure that the Standard tab of components is selected).

  6. View the Object Inspector (F11) and type Hello World! as the caption of the label.

    Caption of label

  7. Press the F9 key to run the program.
    The files will be saved automatically, so you do not need to save them first.

    After a small delay, you should see a form appear.


    Output

  8. Click on the cross at the top right of the form to make the program terminate.
  9. Open the View Unit dialogue (Ctrl+F12) then select uHelloWorld.pas
    The essential code is:
    unit uHelloWorld;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Label1: TLabel;
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    end.
    
    
  10. Press Ctrl+F12 again and view HelloWorld.dpr.
    The essential code in HelloWorld.dpr is simply:
    program HelloWorld;
    
    uses
      Forms,
      uHelloWorld in 'uHelloWorld.pas' {Form1};
    
    {$R *.res}
    
    begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    
    
  11. View Form1 again, right click on it and select View as Text
    The text view of our form is:
    object Form1: TForm1
      Left = 389
      Top = 140
      Width = 171
      Height = 90
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -13
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 120
      TextHeight = 16
      object Label1: TLabel
        Left = 8
        Top = 8
        Width = 74
        Height = 16
        Caption = 'Hello World!'
      end
    end
  12. If you can think of anything we could have made more straightforward for you, then please let us know. If it did not work, then please tell us what went wrong.
Features introduced:
  • New form-based application opened

  • Project and unit named

  • Label component used

  • Text output to screen using label

If you want to, you can quit from Delphi by selecting menu item File > Exit

Alternatively, you can move on to the next topic in this guide. The next program, GreetUser has a little more functionality.

Programming - a skill for life!

How to set up Lazarus to create, edit and run form-based Object-Pascal applications