Delphi Programming for Beginners - Lesson 1
Delphi consists of several parts which work with one another and those parts are:
- Visual Component Library (VCL)
- Run Time Library (RTL)
To make it clear what each part is doing, here’s a little summary. Programmer writes the code (mostly) in IDE where - with help of IDE itself - he also designs the User Interface (of Form) where every object is created by VLC’s components. Consequently, programmer creates reactions on user inputs (for example pressing the button, text input etc.) with Object Pascal (programming language) and RTL. A program is saved in files *.pas, description of a form is saved in *.dfm files. The form itself is then therefore built on pair pas and dfm.
It’s often convenient to separate the repeating code. This code is then reusable from different parts of program (for example from various forms).
Compiler is used for translating pas files into dcu files. At the same time it's checking if the code is error free and some other stuff. If the compilation was done successfully - that means all needed pas files were translated into dcu (I should point out here that only changed pas or files which are dependent on changed files are translated, in other cases already translated dcu files are used), linker is called. Its responsibility is to connect dcu files, attach dfm and other files to them in order to create a final EXE file.
If the optimisation is turned on, the linker also removes unnecessary code (sometimes even blocks of code). Code included into the final binary file is marked by a dot on the left sidebar after the optimisation (= compilation + linking) - in Delphi 2008 by a blue dot - as shown in following picture.
These lines also mark a place where you can set a breakpoint for the debugger (to do so, you have to click on the blue dot). Debugger is used to test and debug the program and a breakpoint indicates where the program should stop. During the time when program is stopped, you can for example examine the variables and once you are done, you can continue with running the program (main menu Run). In the picture below, the breakpoint is set on line 41.
Programming in Delphi is controlled by events. That means that (mostly) you react on a random event (for example pressing Enter). Every VCL component has several events which differ component by component (for example button has OnClick event which is called when the button is pressed).
Apart from events, every component has also properties which specify its behaviour. As an example, button has property Caption which is the button’s text (OK, Cancel and Save are the most common examples of Caption property) or Left and Right (positions on the left and on the right).
Here’s a tiny example. We have a form with 2 buttons (btn1, btn2 - property Name). btn2.Caption is “Yes”. btn1.Caption is “No”. The form in IDE will look like this:
We switch between this form’s code and its visual representation by pressing F12. We are now in a Form view. Click on “Yes” button and in the Object Inspector btn2 will be selected.
Object Inspector is used for editing components. We can there edit properties and events. Changed properties are displayed in bold. Click on Events and double click “OnClick” event to generate the body of this event. Write
between words begin and end so the code looks like in the first picture. This line of code is responsible for displaying the message with text “Yes”.
Press F9 in order to run the program. If you click on Yes button, message with "Yes" should appear. Close the program (you can for example press combination CTRL + F2).
We will do something similar with the second button. So far it’s nothing special so we will do something a little bit more exciting.
We generate body of OnMouseEnter event for button btn2. This event is activated when the mouse pointer is over the button. Inside of the body of this event write following code:
If btn2.Left > 200 then
btn2.Left := 22
btn2.Left := 270;
In order to make the program work properly, btn1.Left has to equal 144 and btn2.Left to 270. You can do it in Object Inspector as well by moving the particular component (the IDE will display component’s properties at that time).
Once the program is running, set the breakpoint (the same way as is done at the first picture) and you’ll see the program stops where we want it to stop (in this case when we have a pointer on the button).
You can download the program belowa at the end of the page. Just before we wrap this up, the jumping button can be pressed by being selected using TAB and confirmed by Space. But we will talk more about this in the future lessons.
Create a program with button which moves to the right side by 20 pixels every time it is pressed but should it touch the right border of the form, set the position to 5.
Sample project regarding homework can be downloaded from here.
About the AuthorIng. Radek Červinka
News from our Sponsors
Active Query Builder VCL Edition is a visual query designer and SQL parser component for WinForms, WPF, ASP.NET, Delphi/C++Builder and Java
Active Query Builder allows users to build SQL queries via the intuitive drag'n'drop interface and to edit them via the powerful text editor with code completion and syntax highlighting, combining these techniques in any order.
It lets parse, analyze and modify complex SQL queries programmatically. It supports tens of SQL language dialects: Oracle, SQL Server, MS Access, MySQL, PostgreSQL, etc. Active Query Builder provides highly productive environment to work with SQL queries for both the end-user and programmer!
Complete solution for using Bluetooth, IrDA, WiFi, WiiMote, Kinect and Serial Ports communication in applications
The Wireless Communication Library is the powerful SDK for software developers.
The Library includes a complete components set for develop applications which should use Bluetooth, IrDA, WiFi or Serial Port communication. Also WCL provides easy way to connect to a Wii Remote controller. Since version 6.11 WCL supports Microsoft Kinect game controller.
The Library can be used with Borland Delphi, Borland Developer Studio, Borland CBuilder, Microsoft Visual Studio .NET, Microsoft Visual C++ and Microsoft Visual Basic 6. The Wireless Communication Library is 100% compatable with Windows XP, Windows Vista, Windows 7 and Windows 8.