Home
Introduction
Courses
Glossary
About

Advanced - Introduction To Delphi

When programming in Pascal goes beyond the DOS command prompt, you'll probably use Delphi. Delphi is a Rapid Application Development (RAD) system for Windows which you can use to compile Windows applications. This course will introduce you to the Delphi programming interface.

Delphi? Does it rhyme with Sci-Fi?

Those of you who are familiar with Windows programming might know Microsoft's Visual Basic (VB), a RAD development system similar to Delphi. Both boast of featuring Visual Development, Object-Orientated Programming (OOP) and user-friendiness. Well then, since they're so similar, why should we use Delphi instead of Visual Basic?

  1. Until VB5, VB generated programs in a half-compiled state called p-code, then attached a p-code interpreter to the program. This causes programs to run more slowly than usual. Even now, with VB5's native-code compiler, Delphi's execution speed is still superior.

  2. Sometimes, you need more than what a program gives you. However in VB, you can't make your own controls within VB itself. In Delphi however, you can easily create your own components without any additional programs.

  3. While VB does deal with Objects and inheritence, it is not a true OOP language. For instance, polymorphism has only been added in VB6. On the other hand, Delphi had always been a true OOP development system.

Even though Delphi is superior over VB, it is still easy to use and user-friendly. In fact, its interface is almost the same as VB's, allowing old users of VB to easily upgrade their skills to Delphi.

The latest version of Delphi currently is Delphi 4. However, because it's too recent, we've been unable to cover it in our course. You might want to learn more about it from the Inprise website at www.inprise.com. Don't worry too much about compatibility though, our code should be compatible with version 2 and above of Delphi, including version 4.

How do we use Delphi?

Draw on the form

There's no better way to learn than to try it out yourself. We'll teach you the three steps of drawing, modifying, and coding by creating an example program which changes the form's caption to 'Hello World!' when you click on a label. First, select the 'File' menu, then select 'New Application'. You'll be presented with a blank new window with dots all over. (You could say it has a bad case of measles, though that's pretty contrived.) This window is referred to as a form. Select the tab labelled 'Standard' and click on the button with an A on it. Drag across the form to create a label. (You might have realized that the dots were a snap-to grid - They'll activate when creating, moving, and resizing components.) If all worked well, you should have a brand new label on the form with the caption 'Label1'. Congratulations! For this simple program, you've already finished the first step: Drawing. Of course, in more complex programs, you would need more than just a mere label...

Properties Now that you've drawn something, you might want to change their properties. Let's change this label's text to be right-aligned. Simple, just use the Object Inspector to change it. Look up 'Alignmentr', and select 'taCenter' from the choices listed. The text centers. Thus in a sense, you're changing what you've just 'drawn'. Much better than your old Crayola crayons, eh? Therefore, to change an object property:
  1. You look it up on the Object Inspector, and then
  2. Change its value on the right
Now, let's try changing the label text. To do this, you change its 'Caption' value to 'Delphi!' Some properties, you notice, have a '+' sign right before its name. This means that that property can be expanded by double clicking on its name - Double-click on 'Font' and change its 'Color' to clBlue. You'll see that the colour of the text changes to blue. You're getting the idea, right? Play around, make the label look as wacky as it can! You can also double-click on some right-hand side values of the Object Inspector to bring up more detailed selection boxes. For example, double-click on the colour value. You'll get a colour selection box.

Edit code for event

Enough. Now that you have customised what you have 'drawn', the next step is to add code to each object. Look back at your Object Inspector. Notice that you've only been looking at the 'Properties' tab, the one you used to change your 'drawing'? Click on the second tab 'Events', and voila! You see the list of events linked to the object that is currently selected. For instance, the 'OnClick' event is activated when the object currently selected is clicked. Considering that you haven't done anything wrong, the following steps would create a 'Hello World!' application.

  1. Click on the form, and go to the Object Inspector
  2. Click on the Events tab
  3. In the OnClick event, you can either type your own procedure name in the value column or just double-click to let Delphi handle the naming complications. (By the way, nobody really names their own procedures. Just double-click to let Delphi do the naming.)
  4. Instantly, the Code Editor comes out for you to edit your code. Notice I said 'edit'? Well, that's because there's already code in your window - Delphi generated all that without you knowing...
  5. Type the following code between the 'begin' and 'end' blocks:
      Form1.Caption := 'Hello World!';
    
  6. Click on the 'Play' button at the top of your screen or press F9.

(Object Name) Form1.Caption (Object Member)

Wait for a moment as your hard disk lights flash... Poof! Instantly, a great window appears on your screen, beckoning you to click on its label. Do so. At once, the title bar changes to say 'Hello World!' It works! Your first Delphi program works! Let's look at your code: First of all, as your code was for the label's OnClick event, your code would only be executed when the label gets clicked. Click anywhere else and it won't work. When you do click it, it sets the Caption member of Form1, which is the form, to become 'Hello World!' Huh? Well, look at it this way. Notice that the statement construct is similar to that of a record? That's because Form1 is an object - Just treat it as a record for now and you'll be fine.

End Of Introduction

Now that you understand Delphi basics, you can go on to learn about some basic Windows Programming concepts. Go on to the next course to learn more.

PreviousNextTop