RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
Delphi

Learn to Program in Delphi – Building and Debugging

Author: Pawe Gowacki

In the first post we have covered the installation of Delphi Starter. Now it is the high time to start using it! The best way to learn something is to start using it to build something useful, but not too complicated. Today we will start creating a “calculator” application that we will use as a vehicle to learn the basics of programming in Delphi.

Understanding the IDE

Probably the most important program that programmers are using is a compiler. Typically a compiler is available in the form of a command line tool that takes as arguments the location of source code files with programs written by a programmer and generates executable files. Another very important program is a debugger. As the name implies it is used to find “bugs” or “errors” in compiled programs. An “Integrated Development Environment” (IDE) combines code editor, compiler and debugger into one application. There are different Delphi compilers that generate executables for different operating systems. In the highest versions of Delphi (Architect and Enterprise) there are separate compilers that generate code for iOS, Android, Mac OS and Windows. Delphi Starter edition is the entry level version of the IDE and contains just the capability to create and debug Windows 32-bit applications.

A Delphi Project

Typically Delphi compiler needs more than one source code file to generate an executable. Delphi IDE organizes all necessary files to generate an executable into a project. The project name later becomes the name of the resulting executable binary file. When you select options to generate a new project, the IDE under the hood generates a number of files for you. Let’s have a closer look into what files and folders the IDE generates.

Project Types

Probably the very first thing to do after starting Delphi is to create a new project. You select “File”, “New” and then you have a choice of creating either a new “VCL Forms Application – Delphi” or “Multi-Device Application – Delphi”. These are two user interface frameworks that you can use with Delphi. The VCL – or Visual Component Library – is the traditional Delphi framework that is there since Delphi 1 and can only be used to create Windows applications. The second framework for creating multi-device applications has been added to Delphi in version XE2 and is called “FireMonkey” or “FMX”. It is a unique technology on the market that let you build natively compiled applications from the same source code for all major desktop and mobile operating systems. In Starter there is only Windows Win32 compiler, but in the future you may want to upgrade to higher versions of Delphi that supports other platforms as well.

In this series we are going to focus on using the FireMonkey framework.

New Multi-Device Project

Let’s create a new multi-device project that later will become our “calculator” app. In the IDE you can do the same thing is many different ways. You can create a new project using the option on the “Welcome Page”, create it from the “File” menu or use “IDE Insight” and just start typing what you are looking for after pressing “Ctrl+.” key combination.

Click on the “New Multi-Device Application – Delphi” option. This will display the wizard screen, where you can select the project template to use. Select “Bland Application” template and click “OK”.

The new empty application project will be created for you. Now click on “Save All” to make sure that all files generated by the IDE are securely saved on the hard drive. You should always make sure that you save project files in a new folder, because there is a number of files and subfolders generated for you. It can have any name. Let’s create a new “DelphiCalc” folder.

First you will be prompted to save the main form of your project. A “form” is an application “screen”. A visual application can have one or more visual forms. Save the main form of application as “uFormCalculator” in our newly created folder. After saving the form the IDE will ask you to save the whole project. Here the name that we will choose will become the name of the executable that we are going to create. Let’s give our project the name “DelphiSuperCalculator”. This could be anything, but the name cannot contain spaces, it needs to start from a character and in general needs to be a valid filename.

On the right side of the IDE there is “Project Manager”. Here you can see all files that make up the project.

Now let’s go to the file system and have a look into our “DelphiCalc” folder. There are two files called “uFormCalculator” created there. One with *.pas and the second with *.fmx extension. The first file contains the source code with the definition of the form class. For now we are not going to go into the discussion of object oriented programming and what the class is.

In the “pas” file you will find the line with {$R *.fmx} statement that tells the compiler to look for the second file with the “fmx” extension. This file is managed by the visual form designer and the programmer does not edit this file directly. At the bottom of the IDE there are tabs called “Code” and “Design” that let you switch between code and form view.

You will also find two files called “DelphiSuperCalculator” in there. One with the “dpr” and one with “dproj” extensions. You can preview the content of the “dpr” file in the IDE by selecting “View Source” option in the “Project” menu. In Delphi the execution of the application always starts from a file that has the “program” keyword in the very first line. By convention the IDE gives this file “dpr” extension. The second “dproj” file is managed by the IDE and contains XML code that drives the MSBuild engine that manages the actual process of compilation. There are also some additional files that are generated by the IDE, but right now they are not critical to understand the basics.

Building the Project

The IDE not only let us build the application, but you can also run it. In the “Project” menu you will find the option to build the application. We can also select “Run” command that will first build the project and then run it. The easiest way is to just click on the green arrow icon under the menu.

Click on the “Run” icon. The IDE will start the process of compilation of the all source code files that make up the project. In the lower pane there will be messages with information what commands and with what parameters have been executed. In a moment you should see an empty form displayed. If you go to the Windows Task Manager you will see that “DelphiSuperCalculator” app is running.

Close the application and go back to the file system. After initial running of the application, the IDE has created a subfolder called “Win32”. This is how the IDE organizes output from different compilers. In Starter there is only “Win32”, but in higher Delphi versions you can switch between other compilers and they will generate other subfolders like for example “Win64”. Under the “Win32” folder there is “Debug” subfolder and there you can find the “DelphiSuperCalculator.exe” file which is our executable application! At this stage our app does not do anything useful but later we will add functionality to it.

Debugging the Project

Another very important functionality of the IDE is debugging. Programming is a mental art. Sometimes what you think what your application should be doing, is not what the compiled application actually does. At such moments debugger is a super useful tool. It let you step through the execution of the program. You can also inspect and change almost all aspects of the environment in which your application executes from the contents of variables that you define to computer processor registers (if you need to).

In order to use the debugger we need to start adding some source code to our project. Let’s do something super simple. In the right-bottom part of the IDE you will find the “Tool Palette” that contains lots of reusable components that are used as building blocks for your applications. Locate “TButton” component and double-click on it.

A new button will be added to the form. Double-click on the button. You will be moved from the Form Designer to Code Editor. The “OnClick” event has been generated for you. Now you can enter between “begin” and “end” keywords the code that will be executed when the end user clicks on the button. Type there one line of code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Hello from Delphi!');
end;

Click on “Save All” and “Run”. If you click on the button, you should see the message.

Go back to the code editor and click on the left side of the “ShowMessage” line in the gutter. That should add a red circle – so called “breakpoint” – and mark the whole line of code in red.

Now click on the button next to green arrow that has a hint “Run with Debugging”.

This will start the program in the debugger. After you click on the button, the program will stop and you will be taken back to the code editor. The IDE will display many new windows. You can “View – Debug” menu to see what debug windows are available. 

What’s Next

That’s it for this time. We have created our first project, run and debugged it. Next week we will continue building our first Delphi “calculator” app!


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile
Related posts
C++DelphiNews

RAD Studio 10.4 Patch 3 is out: VCL Grids, C++ Win64 Debugging, and C++ Android Exception Handling and Resources

C++

What's New in the GetIt Package Manager - June 2020

News

Unexpected popup window when debugging in RAD Studio 10.4: Workaround

C++

RAD Studio 10.4 Now Available, Learn More

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES