Crash Course Delphi, part 2: Forms, Edits, Buttons


Foot2Meter simplified

 
In this lesson we'll create a simplified version of Foot2Meter, the application that was introduced in lesson 1.

For now, the accent will not be on what the program does, but rather on how to start a project, how to save the files, add components, and so on. But very soon, we'll continue in a higher gear.
 
 

Preparation

  1. If you haven't done so yet, download foot2metersimp.zip to \DelphiLand.
    Hold your horses! Don't unzip it now: every project will get it's own directory, and it is best to keep all the original files in \DelphiLand.
  2. Create a new directory: \DelphiLand\Foot2MeterSimp. Of course, you can choose other names for your directories and projects. But if you use the structure that I propose here, it will be a lot easier to understand each other.
  3. Unzip foot2metersimp.zip to directory \DelphiLand\Foot2MeterSimp. Check if you've got the files foot2metersimp.dpr, unit1.dfm and unit1.pas.

Starting at the end

As usual, we'll start with the end: how should our simplified Foot2Meter look like and what should it do?
  1. Open the project Foot2MeterSimp.dpr in Delphi.
  2. Compile the application. If you have forgotten how to do that, have a look at lesson 1.
    As with every succesful compilation, Delphi will start the application in the "environment" of the debugger.
    Just play around with the program.
  3. Notice that Delphi gives an error message and that it pauses the program, if you try to convert an invalid value. Continue your application. Afterwards you do not get a second error message, because in this simplified version of Foot2Meter we are not checking for a valid input (compare this behaviour with lesson 1).
  4. Stop the application and quit Delphi.
  5. Delete all the files from \DelphiLand\Foot2MeterSimp.

Your Turn !

  1. In the File menu, select New, next select VCL Forms Application
    (Note: in Delphi 7 or older, select New Application)
  2. Delphi will start a new project:

    • Delphi's title bar shows the name of the new project: Project1. This means that Delphi created a dpr-file (project file) called Project1.dpr.
      Note: if you start several new projects one after the other during the same session, the second project gets the name Project2.dpr, the next one will be Project3,... Delphi consequently gives names along these lines to new files and new components.
       
    • Notice the window with the title Form1. That window (form) is the basis for your project.
      Delphi applications for Windows are based on Forms. Every GUI application has one or more Forms. A Form is a component in the shape of a window. That's why Bill called it "Windows"...
      On the form you put other components, such as Buttons, Edit-boxes, Radiobuttons, ListBoxes, ComboBoxes and other well known Windows-creatures.
       
    • You also see the Editor window. That's where the source code can be viewed and edited. The source code is the result of the cooperation between Delphi and yourself: for every unit, Delphi creates a template that you can complete.
  3. Unit1 is the name of the unit that goes with Form1. Delphi created the file Unit1.pas, containing the source code for Form1, the main form of the application.

    For the moment, all these files only exist in the RAM of your computer. You have to save them to disk files.

  4. Open the File menu and click Save All.
    In the dialog Save Unit1 as, select directory \DelphiLand\Foot2MeterSimp. Leave Unit1.pas as the file name. The additional data for Form1, such as the locations and dimensions of the components, are saved automatically in Unit1.dfm.
  5. In the next dialog, Save Project1 As, the right directory is already selected. Enter Foot2MeterSimp as file name. Delphi saves the program-data of the project as Foot2MeterSimp.dpr (this file contains the names of the units and the forms that you created, plus the names of other units used by the project, and so on).

Files and file naming

It is best to save all files of a new project as soon as possible. That way, you won't experience nasty surprises as lost projects (what was that name again?), or an existing project being overwritten by a new one (and that's a lot worse...)

Quick Analysis

een korte analyseAnalyzing before you start is half of the game.

Clicking on a button should convert the value in the edit-box from foot to meter, or from meter to foot.

We know that 1 foot equals 0.3048 meter. Thus it seems that 1 meter equals 1/0.3048 foot ;)

Components for this application: 1 edit-box for data entry, 2 buttons (one for Foot -> Meter, one for the reverse), plus a few labels.

Adding the Components

The next steps explain how to add the components to the form:

  1. Bring the form window to the front. Tip: you also can press function key F12, which toggles between the code in the editor and the "form".

    Probably you will see a raster on the form, facilitating the alignment of the components. For reasons of clarity I omitted this raster in the pictures that I show here. On my PC, I switched off the form-raster: about everything can be personalized in Delphi. But don't change too much if you only just started with Delphi, because it's quite complicated to restore the original settings. In the worst case scenario, you would have to reinstall Delphi :-(
  2. In the component palette, click on the icon of the TEdit component. It is on the Standard page of the palette:

  3. Click somewhere in the form. An Edit-box appears on the form, with the text Edit1.
  4. On the form, drag component Edit1 to the upper left-hand corner, right below the title bar.

  5. Click the TLabel component from the component palette.

  6. Move the cursor to somewhere to the right of Edit1 and click: Label1 is placed on the form.

  7. Add another TLabel to the right of Label1. Label2 is born.
  8. Add a TButton component (also on the standard page), somewhere below the Edit-box. A button with the name Button1 appears.

  9. Below Button1, add a second button to the form.
  10. Finally, add a TLabel to the right of Button1. By now, your form should look similar to this:

  11. Drag the components until Form1 more or less matches the picture shown above.

    Probably your form is a lot bigger than the one that I show here. Size the form by dragging the bottom right corner of the window's frame, just as you would do with any other window.
  12. Time to save your work: in the File menu, select Save All.
  13. Let's test. In the Run menu, select Run (or press key F9).

    Delphi will compile and run your program.
    The program doesn't seem to do anything yet, but make no mistake: this is a full blown Windows application! You can size and move the window, enter text in the Edit, click the buttons,...
  14. Stop the application.

Every application contains one or more Forms...
But to every rule there is an exception... ;-) Delphi also allows the creation of applications without forms. And this is the last word you hear about this, because our lessons are about RAD for Windows!
Back to main text

Lesson 1 LESSON 1      TOP   LESSON 3 Lesson 3