In this article, you will learn how to create a new program, save its sources and work with them.
In a
previous tip, we've learned about the development environment. Now it's time to actually create a working program (project). Remember, Delphi does not save everything in a single file, so for each application you create, use a different folder for saving. So go ahead and create some "Hello world" folder. Then open Delphi. The new proect is already started, but i'll show you how to start it manually, because you'll need it in the future. At first, close existing project by clicking
File > Close All in the main menu. Then click
File > New > Application. That's it - you have a new project. Now we need to save it in our newly created folder. But first, i'll explain what files the project consists of.
.pas - Files, that end with ".pas" are called units. They store the actual program code.
.dfm - DFM files store visual layout and some other properties of components. These files are not created for each component, they describe forms. So every time you create a form, a DFM file will be assigned to it.
.dpr - DPR is one per project. This is your actual application. Programs will not run if they don't have a DPR file. Usually you don't have to edit annything in this file, you'll mainly work with PAS files.
.res - These are resources for your project (for example icons).
There are other files, but these are the most important ones. Actually, the first 3 are critical. Your application will still work if you only have all the PAS, DFM files and a DPR file.
So now let's save our first project. Remember that for each form you have, there's going to be one PAS and one DFM file. Since we have only one form yet, we will need to save 3 files - a PAS file, a DFM file and a DPR file. Luckily, DFM files are saved automatically when you save a PAS file. So click on
File > Save All ... in the main menu and the first dialog will ask you to specify the file for your form (PAS). Find the "Hello world" folder and save this file as "MainUnit.pas". The second dialog will appear, asking to save the DPR file. Save it in the same folder as "Hello.dpr". Do not use spaces in naming files.
So we finally have a project. Hit
CTRL+F9 on your keyboard, then open the "Hello world" folder and you will see a "Hello.exe" in it. Congratulations - you've just compiled your first application! Every time you want to convert your source code to an EXE file, press
CTRL+F9. However, if you double-click the EXE file, you'll only see an empty form. That's ok, because we have done no programming yet. We will start learning how to do this in the next tutorial. For now, let's just prepare the interface.
In main menu, click
File > New > Form. You will see another form appear and in the code editor (
F12), another tab will appear. You need to press
File > Save All ... again. A dialog will appear, asking to save another PAS file. Name it "AnotherUnit.pas". Now click on the second form and add a button to it (find and select
TButton from
Components palette and click anywhere on the form). In the
Property inspector, find a property, called
Caption and change it to "Close Me". Now select the first form and add a button with
Caption "Show second". Hit
CTRL+F9 again and open your "Hello.exe". You will still see a single window, but now it has a button. That's also ok, because all other forms are invisible until you call them yourself.
Note: You can also execute your program by just pressing the
F9. This executes the program in the
Interpreter, instead of executing the EXE file. But this method is better when you are looking for some errors that you may have done in your code.
So we've learned how to create a new application and now let's start learning the real coding
in the part 3 of the introduction tutorials.