Crash Course Delphi - Lesson 3


Crash Course Delphi, part 3: properties and events

 
Time to bring some life into our Delphi project.

Let's change the properties of some of the components: their caption, color, size, font, and so on. And let's add just a little bit of code. Finally, I hear you sigh... isn't that all is programming about...? But Delphi is at least as much about components and their properties!
 

The Object Inspector: Properties and Events

  1. In the Object Inspector you can change the properties of the components at design time.
    If you don't see the Object Inspector, open it thru' the command in the View menu, or press key F11.

    Object Inspector
  2. Make sure that Form1 is selected in the Object Inpector, as shown in the picture above (select Form1 in the Inspector's listbox). Then, click on the Caption property and type: Euro Currency Convertor. The text in the title bar of the "form" (the window) changes as you type.
  3. On the form, click Label2, in order to select it. In the Object Inspector, change the label's Caption to equals (you can also select component Label2 in the Inspector's listbox). Then, change the Caption of Label4 to Euro and change the Caption of Label6 to BEF.
    Change the Caption of Button1 to Euro -- BEF.
    Change the Caption of Button2 to BEF -- Euro.
  4. The names of our components are not very descriptive. Let's at least change the names of the components that will be used in the program's source code. That is, the components whose properties will be changed or used when the program is running.
    Select Edit1 and change its' Name property to InputEdit.
    Likewise, change the names of the following components:
       Label1 : CurrLabel
       Label3 : EuroLabel
       Label5 : BEFLabel
       Button1 : EuroButton
       Button2 : BEFButton
  5. Change the Text property of InputEdit to 100. This assures that the program has a value to start working with, i.e.: "100 currency units".
  6. In the Object Inspector, you also indicate the events to which a component should respond. Select EuroButton. Next, select the Events tab of the Inspector: you'll see the events OnClick, OnEnter,...
  7. Double-click in the white field to the right of the OnClick event. Delphi will write EuroButtonClick into this field. That will be the name of the Event Handler, the routine that is executed each time EuroButton is clicked.

    event OnClick
  8. In the Editor, Delphi has already created a template for this Event Handler:
     
    procedure TForm1.EuroButtonClick(Sender: TObject);
    begin
    
    end;
  9. Let's experiment for a moment with some (temporary) source code. Between the words begin and end of the Event Handler, you type the code to be executed when EuroButton is clicked:
    CurrLabel.Caption := 'Euro';
    EuroLabel.Caption := InputEdit.Text;
    The entire Event Handler should read as follows:
     
    procedure TForm1.EuroButtonClick(Sender: TObject);
    begin
      CurrLabel.Caption := 'Euro';
      EuroLabel.Caption := InputEdit.Text;
    end;

    Attention! The word Euro is between single quotes, not double quotes!
  10. Compile and run the program. Type something in the Edit-box and click the EuroButton: the text 'Euro' appears in CurrLabel and the text of the Edit-box appears in EuroLabel. In Delphi-language:
    • the string 'Euro' is assigned to the property Caption of CurrLabel;
    • the property text of InputEdit is assigned to the property Caption of EuroLabel.
  11. Stop the application and save the project (Save All).

 


Well, I hope you're beginning to see the idea behind this tutorial: getting you up and running in as little time as possible -- practice first, and then theory. At first, I'll show you what Delphi does. And when you're becoming too curious, I'll tell you the why and the how. Sounds fair? At least, sounds fun, I hope ;-)
So, up to lesson 4, where we shall make things happen in runtime : let's code!



You can freely download lessons 1 to 3 of our Crash Course Delphi and the associated source code files.

Members of the DelphiLand Club can also download the other lessons and our specific tutorials, including all fully commented source code files.

Lesson 2 LESSON 2     TOP   LESSON 4 Lesson 4