1. Technology

Zarko Gajic your About Delphi Guide About Delphi Programming
From Zarko Gajic, your Guide to Delphi Programming
http://delphi.about.com

A Beginner’s Guide to Delphi Programming - Chapter 3
An overview of application development with Delphi, including creating a simple project, writing code, compiling and running a project. Also, find out how to ask Delphi for help.
http://delphi.about.com/library/weekly/aa050702a.htm

Welcome to the third chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
An overview of application development with Delphi, including creating a simple project, writing code, compiling and running a project. Also, find out how to ask Delphi for help.

   Creating your first 'Hello World' Delphi Application
It's time to create a simple example in Delphi now. When you start Delphi, a default project is created with one form. This default project automatically creates a blank form, with its associated unit file and a project file, among others.
To get started, from the beginning, close anything that's open by choosing File | Close All from the main menu.

File | New... Before you create your first Delphi project, you need to know what you want to develop; a DLL, MDI application, SDI application a CLX application (for Linux) or something else. To start without all the bells and whistles we'll create a standard SDI Windows application. Simply point your mouse to File | New and select Application. This creates a new project group with a single application in it.

Project Manager Window

The new project contains an empty form, a unit (associated with its form), and a project file. As you develop and build your application, new files will be created and added to the project. The project files are listed in the Project Manager window, display it by selecting View | Project Manager from the main Delphi menu. With the Project Manager, you can easily visualize how all your project files are related. If you share files among different projects, using the Project Manager is recommended because you can quickly and easily see the location of each file in the project.

Application vs. CLX Application
With some versions of Delphi (supposed Delphi 6 Professional or Enterprise), you can build and develop cross platform applications that can be ported to Linux and compiled with Kylix. To develop a CLX application, instead of standard Windows application, you could pick CLX Application from the File | New menu. The Delphi IDE is similar to one when you build Windows applications, except that the Component palette changes dynamically to show the objects that are available for use in Linux CLX applications.

Since this course is about Delphi for the Windows platform, we will be exploring Delphi programming from that point of view. However, if you have Kylix and want to join this course you are of course encouraged to do so. Even though, my intention at this stage of this Course, is not to explain differences between CLX (Linux) and VCL (Windows) development you should know that there are no reasons why you should not join the course and just have in mind that when we talk about, let's say, form1.DFM you think form1.XFM.

   Hello Delphi
Now that we've created a project, we can begin work on our first application. This first application will be pretty simple - we'll change the caption of the (main) form once the application is executed. The change will be initiated from code - no user interaction will be necessary.

To add the code that changes the caption of the form we need to *call* the Code Editor window. If you have the Project Manager displayed on the screen, double click the Form1. This will bring up the Form designer window to the front. Another way to bring the Form1 to the front of the screen is to select Form1 from the Window menu. Once Form1 is on top and active, double click it. This action has the following result: the Code editor is positioned on the top of the screen and Delphi creates the skeleton code for the new event handler.
Note: another way of achieving the same result is to activate Form1 in the Object Inspector, select the Events tab and double click in the OnCreate column value.

As stated in the second chapter of this course, each form has a collection of events – such as a mouse click, keypress, or component activation – for which you can specify some additional behavior. In this case the event is called OnCreate. This event occurs when the form is created.
The skeleton code looks like:

procedure TForm1.FormCreate(Sender: TObject);
begin
//this is where your code goes
end

For the moment do not get bothered with the meaning of the text you see.

Now alter the code so that it looks like:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := 'Hello Delphi! ' + DateTimeToStr(Now);
end

Running a project for the first time
To see the results of this action, you need to (successfully) compile and run you project. From the Run menu choose Run or press F9. The compiler will try to build the project and execute your application. If the compiler encounters an error, it displays an Error dialog box. When you choose OK to dismiss the dialog box, the Code editor places the cursor on the line of code containing the error.

Show compile progress

Note: if you want to see progress reports while your program compiles, you'll need to check the "Show compiler progress" check box in the "Compiling and running" section on the Preferences page of the Environment Options dialog box. Call this dialog box by selecting Environment Options from the Tools menu.

Hello Delphi

If everything goes well (it should) your application is executed and you see a blank form on the screen. Note several things. First, the form is blank - there are no dots that make up the grid you see when designing the form look. Second, there is a new button on the Windows Task Bar - when you point to it you'll see that it has the Project1 caption. Third, the caption of Delphi IDE is changed from "Delphi 6 - Project 1" to "Delphi 6 - Project 1 [Running]". And fourth, most important for us, the caption of the form is Hello Delphi ! + *date and time of the execution*.

There is not much you can do with this window, you can move it resize it and finally close it. Every time you (compile and) run this project a form caption will say Hello Delphi with the date and time of the execution.

Ok, I know this is not a lot, but be patient - this is your first project - it is not supposed to do something meaningful.

   Saving the project
To properly get the job done, you should save the project, along with all its associated files. To save the current form design and its code, select File | Save All from the main menu bar. By default, Delphi opens the Projects folder. I suggest you to create a new folder (inside the Projects folder) for your project. Let's call it "HelloExample". While in the Save As dialog, open the newly created HelloExample folder and save the following files:
. save Unit1 as MainUnit.pas
. save Project1 as HelloProject.dpr

Note 1: When you have saved the unit file, the corresponding form was saved as MainUnit.dfm
Note 2: In the Code Editor window, Unit1 is now referred to as MainUnit.
Note 3: Since you have saved the project with the *new* name, if you run your application now, the button on the Task Bar will say "HelloProject". Of course the name of the application and the name of the project do not need to be the same, later we will see how to change the name of a Delphi application.

HelloWorld Folder

Note, if you open up the HelloExample folder in the Windows Explorer, you should find several files inside it. These are MainUnit.pas, MainUnit.dfm and several others. The most important file inside this folder is the HelloProject.exe. This is your applications executable file, if you double click it you'll execute it. If you want to "install" your application on another machine this is the only file you need to copy.

   Getting HELP from Delphi
Let's stop for the moment to explore ways to get help from Delphi in situations when help is necessary. First of all, Delphi is supplied with extensive documentation. If you do not have the printed manuals, those that came (as PDF) with the installation will do. As stated in the first chapter of this course, the books include:
. Quick Start - a brief introduction to Delphi,
. Object Pascal Language Guide - a complete reference to the underlying Delphi programming language, and
. Developers Guide - which covers advanced topics, from creating database applications to creating your custom components.

Beside printed materials, Delphi holds a great deal of information in the Help system. Even though you'll need to learn how to use it, it is really worth it - there are many code examples to help you understand all the nuts and bolts of Object Pascal programming. What's more, context-sensitive Help is available from nearly every portion of the Code editor. To get context-sensitive Help from the Code editor window simply place the cursor on the property, event, method, procedure or type for which you want Help, then press F1.

Try it. Position the mouse cursor inside the word "Caption" in the Code Editor (the word Caption you typed in the only example so far) and hit the F1 key.

Help system

Once you press the F1 key, a pop up window will ask you to specify more exactly what you want to know. Here comes the hard part: how in the world you know what topic to pick. The "problem" lies in the fact that, in Delphi, many components have properties of the same name (and behavior). To get the help on Form Caption property you need to pick TControl.Caption. Why TControl, when you are working with Form not something called TControl? Well, for the moment this is hard to explain, let's just say that Form derives from something called Control and that Control has a Caption property. What you will find out is that in general, Caption is used for text that appears as a window title.
But how will you know what to pick? There is a solution. Point to Object Inspector, Properties page. Select the property you want to find out about and than press F1.

   Some exercises for you...
Since this Course is an online course, there is much you can do to prepare for the next chapter. At the end of each chapter I'll try to provide several tasks for you to get more familiar with Delphi and the topics we discuss in the current chapter. Here are some exercises for you:

0. Learn about the Name property of the Form object. Note that the Name property should tell you what the form does.
1. Explore the Object Inspector and try to figure what properties relate to the Form positioning on the screen (Left, Top, Width, Height, ...) and what happens when you alter them at design time.
2. Try to change the color of the Form from the Object Inspector (Color property)
3. Learn about the BorderIcons and BorderStyle properties and how they relate to visual representation of the Form at run time.
4. Find what exactly DateTimeToStr is used for.
5. Be sure not to miss the next chapter!

   To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the third chapter, in the fourth chapter, you'll create a second simple application allowing you to learn hot to place components on a form, set their properties, write evnet handler procedures to make components work together.

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

{
Zarko Gajic, BSCS
About.com Guide to Delphi Programming
http://delphi.about.com
email: delphi@aboutguide.com
free newsletter: http://delphi.about.com/library/blnewsletter.htm
forum: http://forums.about.com/ab-delphi/start/
}

©2014 About.com. All rights reserved.