Delphi Programming

So you just installed Delphi (maybe even Turbo Delphi Explorer) and ask yourself, what do I do next?

What about writing your first application? Lets start with a classic: Hello World.

  • Start Delphi
  • Under File select "New -> VCL Forms Application" (or similar, it depends on your Delphi version what it is being called), you will get a project called "Project1" containing a unit called "Unit1" which represents an empty form displayed in the Project Manager.
  • Save your project using the "Save Project as ..." entry in the File menu. Note that Delphi will ask you to save the unit first, not the project. So you will get two file save dialogs. You can pretty much save the files wherever you want, but the file names are limited to valid Pascal identifiers, that is, they must start with a letter, followed by letters or digits or an underscore and must not contain any special characters. Save the unit as "w_MyFirstProjectMain" and the project as "MyFirstProject". You will notice that the new names are reflected in the Project Manager. I use a convention that prefixes units that contain forms with "w_" and units that contain only code with "u_". Others may disagree and use suffixes like "Form" and "Unit" or nothing at all. Just pick the convention you like best and stick with it. If you work for a company you may find that they have got their own particular conventions, but then you will probably not be reading this but hopefully get a real tutorial.
  • Compile and run your program using the "Run" toolbar button, that's the one with a green triangle that points to the right. You will get an empty form with a caption of "Form1". Pretty boring, isn't it? Exit the program by closing the form.
  • Double click on "MyFirstProjectForm" in the project manager to bring it to the front (if it wasn't already)
  • Now let's add two buttons to the form:
    • If you are using Delphi <=7 you will find buttons in the "Component Palette" which is located just below the main menu. On the Tab called "Standard" the small button with the caption "OK". Drag it onto your form and place it wherever you like. When you are done, add another button.
    • If your are using Delphi >= 2006 you will find buttons in the "Tools Palette" which is located on the right hand side in the category called "Standard" the small button with the caption "OK". Drag it onto your form and place it wherever you like. When you are done, add another button.
  • Give your first button the name "b_Exit" and the caption "Exit". To do that, open the "Object Inspector" (F11). It displays a list of name value pairs. Locate the row with the name "Name" and edit the value to say "b_Exit". Likewise locate the row with the name "Caption" and edit the value to say "Exit". You will notice that the caption of your first button, which started out as "Button1" now says "Exit".
  • Repeat these steps to give your second button the name "b_Hello" and the caption "Say Hello". Note that component names (buttons are a special kind of component) are limited in the same way as project names, they must be a valid Pascal identifier. I for one use the prefix "b_" to denote a button, others may disagree and prefer to prefix them with "btn" or use a "Btn" or "Button" suffix instead. But Delphi itself doesn't really care one way or another. I have found that it helps to use prefixes so I can easily identify components when I access them in code (which we will do in a few seconds).
  • Add a label and an edit field to your form. Those components are also located on the "Standard" tab / category. Just drag them onto the form. Put the label on top of the edit field. So they look like this:
  • Change the label's name to "l_FirstName" and its caption to "First Name"
  • Change the edit field's name to "ed_FirstName" and set its text to an empty string (Text is also a property of the component which you can change in the "Object Inspector".
  • Change the caption of the form to "Main - My First Project" and its name to "f_MyFirstProjectMain". You do that by clicking on the form background to select the form (and no component) or select a component and press Esc as long as it takes that the form itself is selected. Then you can change the caption in the "Object Inspector"
  • You might want to readjust the positions of the four components you have so far dropped on your form. Just drag them where you like them. You can also resize them by selecting the component and drag the surrounding frame. The same applies to the form itself. My form now looks like this: Creating Your First Application MyFirstProjectMainForm
  • Now compile and run your program. You will see that the form is now no longer empty. You can type text into the edit field but the buttons don't do a thing. Let's change that. Exit the program by closing the form.
  • Let's start with the simpler function: The Exit button should exit the program. Double click it. Delphi will automatically do the following things for you:
    • Add a new method "b_ExitClick" to the form's class declaration.
    • Add this new methd as the OnClick event handler of your button
    • Create an empty procedure implementation like this:
procedure Tf_MyFirstProjectMain.b_ExitClick(Sender: TObject);
   <cursor is here>
    • Open the code editor and place your cursor where you will want to write the code.
  • Add the following line between "begin" and "end":
  • Compile and start your program again. The form will appear just as last time but now the "Exit" button has got a function: It closes the form. And since your program has only got one form, this is the main form and closing it will also exit the program.
  • Let's add some more code, this time to the "Say Hello" Button. Double click on it and add the following code:
procedure Tf_MyFirstProjectMain.b_HelloClick(Sender: TObject);
  FirstName: string;
  Greeting: string;
  FirstName := ed_FirstName.Text;
  Greeting := Format('Hello %s!', [FirstName]);
  MessageDlg(Greeting, mtInformation, [mbOK], 0);
  • Compile and start your program again. Type your first name into the edit field and press the "Say Hello" button. It will display a message box with the Text "Hello <your first name>!" and an OK button. Pretty cool, isn't it? (Yeah, right, but it's a start. ;-) ). Press OK and Exit to exit the program and go back to the code editor to have a look what this code actually does:
  FirstName: string;
  Greeting: string;

Declares two string variables. A variable of this type can contain text.

  FirstName := ed_FirstName.Text;

Takes the text you typed in to the edit field by reading its Text property and assigns it to the variable FirstName.

  Greeting := Format('Hello %s!', [FirstName]);

Calls the Format function to combine the template string "Hello %s!" with the content of the FirstName variable and assigns it to the variable Greeting.

  MessageDlg(Greeting, mtInformation, [mbOK], 0);

Calls the MessageDlg function and passes it the variable Greeting. Also it passes mtInformation to make the messagebox one of the type information, passes [mbOK], which is a set containting the element mbOK to tell it that there is only one button, the OK button, and 0 which is the help context (I won't go into that right now).

That's it, you just wrote your first program. If you have tried something similar with other programming languages you will probably have noticed that Delphi makes writing GUI applications quite simple.

If you want to have a look at some more complex programs, see the example projects that come with Delphi. You can find them under

c:\program files\borland\delphi<your version>\demos

for Delphi <=7 (don't know about Delphi 8, I never used it) and under

c:\program files\borland\bds\<your version>\Demos

for Delphi >= 2005

Next Steps[]

For the next steps in Delphi programming, see Beginning Delphi