The Delphi language and IDE
Delphi is considered to be an object-oriented, visual programming environment used to
develop 32 and 64 bit applications for rapid application development (RAD). The RAD
package or studio used allows for the design of user interfaces, for the generating and
editing of code and for the compiling and debugging of applications. The tools available in
the IDE will depend on the version of the RAD studio installed. Delphi can be used to
create visually enhanced applications for Windows, Mac and iOS operating systems.
Delphi has two environments, the text based environment, is known as a “console”
application called Pascal and a Graphical User Interface, (GUI) called Object Delphi. We
are studying Object Delphi.
The Object Delphi IDE comprises several tools, menus, commands, components and
properties that allow for the design and the execution of a program.
Delphi IDE example
Title Bar Structure Main Menu Standard Toolbar Form designer
Object inspector Views Tool Palette
Compiled by Georgina Ramsamy
Different views in a Delphi application
You will see these views at the bottom of the Delphi application screen.
Code Allows the user access to the coding of the application
Design Allows the user access to the GUI design of the application
History The user can view the dates and times when the application was
created and opened thereafter.
The F12 button is used to switch between the Code view and the Design view.
You may also click in the respective sheets to gain access to these views.
Tool Palette
The Tool Palette is made up of items to help you develop an application. The items
displayed depend on the current view, that is, if you are currently viewing the design or the
source code of the application.
For example, if you are viewing a form in Design view, the Tool Palette displays controls
and components that are you can place onto the form.
If you for example select an item from the tool palette, it will allow you to select a
component listed in this item.
Compiled by Georgina Ramsamy
NB.
You don’t need to memorise where each component is saved or listed. You can search for
the component in the tool palette and the IDE will locate it for you.
Example: I searched for a component called RichEdit in the tool palette, it showed two
results, one from Win32 and the other from Data Controls.
You can double-click on a control (component) to add it to your form.
If you are viewing code in the Code Editor, the Tool Palette displays code segments that
you can add to your application.
Compiled by Georgina Ramsamy
Object Inspector
This allows you to customize the properties of and to create event handlers for the
components in the application created. Each form and each object has a set of properties
such as colour, font, size, position, caption etc that can be modified in the Delphi IDE or in
your code. Each component also has a collection of events such as a mouse click or
component activation for which you can specify the behaviour of the component during run
time. The Object Inspector has two tabs (Properties and Events) that displays the
properties and events for the selected object (component) and allows you to change the
property value or select the response to some event.
An example of the object inspector for a form
Properties of the Object Inspector Events of the Object Inspector
When a new project is created in Delphi, the following files must be saved.
Extension of file Explanation
.dfm This file is a form file that is created automatically when a
new form is created. It contains the properties of the
components used in the form.
.pas A Pascal file where the coding of the unit is saved
.dproj A project normally has a single “.dproj” file that can contain
many unit files
Compiled by Georgina Ramsamy
An example of a Unit file(.pas)
Notice that the Pascal file is made up of the interface and the implementation section.
Interface:
Consists of the uses section that lists the different pre-defined units (classes) used.
A type declaration that specifies the form name/s, private and public declarations.
A var declaration for declaring global constants, variables, procedures and functions.
Implementation
This section contains the actual code for the unit.
An example of a Project file(.dproj) An example of a part of a .dfm file
Compiled by Georgina Ramsamy
NETBEANS DELPHI Delphi Tool Palette
Panel without a Standard
heading
Panel with a heading Standard
Label Standard
Text Field Standard
Button Standard
Text area Win32
Combo box Standard
List Box Standard
Check Box Standard
Radio Button Standard
Radio Group Standard
Spinner Samples
Slider Win32
Table Additional
Compiled by Georgina Ramsamy
Notes:
Delphi Output Area:
The TRichEdit or the TMemo component can be used as an output area.
The problem with TMemo is that it does not allow for formatting
and alignment. It is therefore recommended that the TRichEdit
component is used.
Naming Components
Ideally, three letters should be prefixed to a component name.
Prefix of components
Component Prefix
TLabel lbl
TButton btn
TPanel pnl
TGroupBox grp
TEdit edt
TRichEdit red
TCombobox cmb
TListBox lst
TCheckbox chk
TRadioGroup rgp
TRadioButton rbt
TSpinEdit spn
TTrackBar trk
TStringGrid sgd
DATA TYPES
Netbeans Delphi
int integer
double real / single / double
boolean boolean
char char
string string
The real data type is widely used and is recommended rather than single and double.
Variables
Variables must be created before they are used.
Compiled by Georgina Ramsamy
Naming conventions should be followed, however, not conforming will not cause your
program to fail, but it will make your program more difficult to read, follow and debug.
Variable names should:
Start with a letter, $ or underscore.
Begin with a letter in lowercase. (By convention)
Have the first word in lowercase, the first letter of remaining words in uppercase.
Use an underscore to join words.
Be relevant, appropriate and should describe the objective of the variable.
Variable names cannot:
be reserved words.
start with a number.
have whitespaces or hyphens for separation.
Reserved words in Delphi
and destructor goto nil procedure string write
array div if not program then xor
begin do implementation object public to
break downto in of read true
class end inline on record type
case else interface operator repeat unit
const false label or set uses
constructor file local packed shl var
continue function mod private shr while
Global variables and local variables:
Global variables are created in the main form under the interface part of the class, and
can be accessed throughout the form / class.
Example below: sCompanyName has global scope.
Compiled by Georgina Ramsamy
Local variables are created inside a procedure / function or in an event handler of a
component. These variables can only be accessed in the segment in which it is created.
Example below:
sName, iAge and rHeight have local scope.
Notice that the variables names is also an indication of the data type it contains.
Variable name Data type
iAge integer
rHeight real
bFlag boolean
cGender char
sName string
Assigning values to variables
Use the symbol := to assign data to a variable.
Examples:
sName := ‘Harry’;
iAge := 15;
rHeight := 1.72;
Create a new project called Tutorial1.
Note all changes to the components on the form will take place in the Object Inspector.
The properties in the Object Inspector are in alphabetical order.
An educator at a school is capturing profiles of learners at the school.
We want to design a form that appears as follows:
Compiled by Georgina Ramsamy
To select a component from the tool palette, simply double click or drag and drop the
component onto the form.
Make a folder called Delphi projects.
Make a subfolder called Tutorial1.
Open the Delphi program so we can create an application.
Follow these steps:
File New VCL Forms Application – Delphi
A new form will appear
Go to File Save As
Browse to the Delphi Projects folder that you created Open the Tutorial1 folder
Compiled by Georgina Ramsamy
Save as the filename Tutorial1_U.pas
This is saved as the Pascal unit file
which contains the code for the application.
We also need to save the application as a project file.
Go to File Save Project As
Ensure that you are in the same folder
Save as the filename Tutorial1_P.dproj
This is saved as the Project file which
contains all the GUI features of the project.
Once the project is saved properly, you can design the application. Click on the
save icon to update and to save changes to the form as you progress.
Note: It is not compulsory to follow this sequence when saving a project; it is also
possible to save to the folder at any time using the same steps.
Design the components on the form.
1. Form
Make use of the new VCL form created. Change the caption to Components and
the name to frmComponents. To do this, go to the Object Inspector and change the
following properties:
Compiled by Georgina Ramsamy
Property Explanation Object Inspector
Caption to Components The heading at the top of the form
Name to Use this name if required in coding
frmComponents
2. Group box (Panel with a heading)
Place a TGroupBox from Standard components onto the form.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to Input area Heading of group box is Input area
Name to grpInput Use this name if required in coding
Font to Ariel Bold The appearance of the heading in the
Size to 11 group box will change accordingly
Colour to Black
3. Label
Place a TLabel from Standard components onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to First name Label1 will now read First name
Name to lblFirstname Use this name if required in coding
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
Place another TLabel for surname onto the group box grpInput.
Property Explanation Object Inspector
Caption to Surname Label2 will now read Surname
Name to lblSurname Use this name if required in coding
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
Place another TLabel for height onto the group box grpInput.
Compiled by Georgina Ramsamy
Property Explanation Object Inspector
Caption to Height Label3 will now read Height
Name to lblHeight Use this name if required in coding
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
Place another TLabel for age onto the group box grpInput.
Property Explanation Object Inspector
Caption to Age Label4 will now read Age
Name to lblAge Use this name if required in coding
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
4. Text Box
Place a TEdit from Standard components onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Text must be cleared Edit box must be blank
Name to edtFirstname Use this name if required in coding
Font to Ariel The appearance of the text typed in
Size to 11 the edit box will change accordingly
Colour to Black
Place another TEdit for surname onto the group box grpInput.
Change the following properties.
Property Explanation Object Inspector
Text must be cleared Edit box must be blank
Name to edtSurname Use this name if required in coding
Font to Ariel The appearance of the text typed in
Size to 11 the edit box will change accordingly
Colour to Black
Place another TEdit for height onto the group box grpInput.
Change the following properties.
Compiled by Georgina Ramsamy
Property Explanation Object Inspector
Text must be cleared Edit box must be blank
Name to edtHeight Use this name if required in coding
Font to Ariel The appearance of the text typed in
Size to 11 the edit box will change accordingly
Colour to Black
5. Spinner
Place a TSpinEdit from Samples onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Name to spnAge Use this name if required in coding
MinValue The lowest value that can be selected
In the spinner
MaxValue The highest value that can be
selected In the spinner
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
6. Combo box
Place a TComboBox from Standard components onto the group box grpInput..
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Text to Grade Heading is Grade
Name to cmbGrade Use this name if required in coding
Items
Click in the ellipses ( )
Add 8,9,10,11,12 to the String List
Editor. These will become the
options that appear in the combo
box.
Font to Ariel The appearance of the text in the
Size to 11 combo box will change accordingly
Colour to Black
Compiled by Georgina Ramsamy
7. Radio Group
Place a TRadioGroup from Standard components onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to Gender Heading is Grade
Name to rgpGender Use this name if required in coding
Items
Click in the ellipses ( )
Add Male and Female to the String
List Editor. These will become
Radio buttons inside the radio
group.
Font to Ariel The appearance of the text in the
Size to 11 radio group will change accordingly
Colour to Black
8. List box
Place a TListbox from Standard components onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Name to lstSport Use this name if required in coding
Items
Click in the ellipses ( )
Add Soccer, Netball, Hockey and
Cricket to the String List Editor.
These will become options to
choose from in the list box.
Font to Ariel The appearance of the text in the
Size to 11 radio group will change accordingly
Colour to Black
Place another TLabel onto the group box grpInput.
Property Explanation Object Inspector
Caption to Days Absent Label4 will read Days Absent
Name to lblDaysAbsent Use this name if required in coding
Font to Ariel The appearance of the label on the
Size to 11 form will change accordingly
Colour to Black
Compiled by Georgina Ramsamy
9. Trackbar (Spinner)
Place a TTrackBar from Win32 onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Name to trkDaysAbsent Use this name if required in coding
Min The lowest value that can be
selected
Max The highest value that can be
selected
PositionToolTip Will show the value selected in a
tool tip box in the direction of the More directions
selection made when the program
is run.
10. Check box
Place a TCheckbox from Standard components onto the group box grpInput.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to Prefect Caption of the check box
Name to chkPrefect Use this name if required in coding
Font to Ariel The appearance of the caption in
Size to 11 the check box will change
Colour to Black accordingly
11. Button
Place a TButton from Standard components onto the form.
Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to Button Caption will now read
Extract and display Extract and display
Name to Use this name if required in coding
btnExtractAndDisplay
Font to Ariel Bold The appearance of the button on
Size to 11 the form will change accordingly
Colour to Black
Compiled by Georgina Ramsamy
Place another TGroupbox (panel with heading) from Standard components onto the
form. Go to Object Inspector, change the following properties.
Property Explanation Object Inspector
Caption to Output area Heading of group box is Output area
Name to grpOutput Use this name if required in coding
Font to Ariel Bold The appearance of the heading in the
Size to 11 group box will change accordingly
Colour to Black
12. Output area
Place a TRichEdit from Win32 onto the group box grpOutput. Go to Object Inspector,
change the following properties.
Property Explanation Object Inspector
Lines must be cleared The Lines property must be left blank
unless you want something to be
displayed as a default value.
Name to redOutput Use this name if required in coding
Font to Ariel The appearance of the information
Size to 11 displayed in the output area will
Colour to Black change accordingly
Compiled by Georgina Ramsamy
13. BitButton
A TBitButton has all the features of a button and also has the ability to add a glyph. A
glyph is also known as a special symbol or icon.
The TBitButton is generally used for a specific purpose. Examples are as follows:
TBitButton Symbol
Abort
All
Cancel
Close
Help
Ignore
No
Yes
OK
Retry
The TBitButton is a part of the Additional Components in the Tool Palette.
After placing the TbitButton on the Form, select the “Kind” property from the Object
Inspector to customise the type of button you require.
Note that the “Close” BitButton was used in the GUI given.
Escape Sequences
Symbol Function
#9 Used to leave a Tab space depending on the Tabcount created
#13 Used as a line feed to move onto a new line
Compiled by Georgina Ramsamy
Set data in, and Get data from components
Compiled by Georgina Ramsamy
Conversions:
From To Function Example
String Integer strToInt iYear := strToInt(sYear)
String Real strToFloat rHeight := strToFloat(sHeight)
Integer String intToStr sCount := intToStr(iCount)
Real / Double String FloatToStr sDiscount := FloatToStr(rDiscount)
We are now ready to code the “Extract and display” button in the GUI.
Click in the button.
You will see the following code:
All variables must be declared before the variables are used.
Note the word “var” is used to indicate the variables used for this button.
Code to extract the information from the components and to display in the output area.
Compiled by Georgina Ramsamy
Notes:
Line number Explanation
52 The word “begin” means the start to a segment of code.
There should be a corresponding “end” for every “begin” used.
53 Extract the first name from the first name edit box as a string.
54 Extract the surname from the surname edit box as a string
55 The data extracted from the edit box has a return type of string.
Since height is declared as real, the string value is converted to
float/real and then stored in the height variable.
56 Extract the age value from the spinner as an integer
57 Extract the item selected from the radio group.
If radio buttons were used instead of a radio group, do the following to
extract the gender.
If rbtMale.Checked = true then
begin
sGender = ‘Male’;
end
else
begin
sGender = ‘Female’;
end;
Compiled by Georgina Ramsamy
58 The data extracted from the combo box has a return type of string.
Since grade is declared as an integer, the string value is converted to
integer and then stored in the age variable.
59 Extract the sport selected from the list box.
60 Extract the number of days absent from the track bar (slider).
At runtime, the tool tip text will display the numbers in the slider.
61 If the prefect checkbox is selected
62 Start to the if statement
63 Set the prefect status to ‘Prefect’
64 End
Note: There is no semicolon at the end of this line as this “end” does
not denote the end of a line. Whenever the word “else” is used, there
must be no semicolon at the end of the previous line.
65 else begin
66 Set the prefect status to ‘Not a prefect’.
67 End of the if statement
70 Clears the output area (redOutput).
71 Display the name heading and the value stored in the name variable.
73 Note that the height variable has to be converted to a string before it is
displayed.
74 The age variable has to be converted to a string before it is displayed.
80 Indicates the end of the button segment
Code for the “Close” BitButton.
How to run the application:
Press F9 OR select the icon in the Tool bar OR select the “Run” option from the
Menu Bar.
Compiled by Georgina Ramsamy
An example of the sample data in the Input area:
If the information above was entered, the output should be as follows:
Notice the output is scattered and inappropriate.
In order to align the information of the TRichEdit componenent, a tab count must be
created.
Setting Tabs in a TRichEdit component
Step1: Specify the number of Tab stops you require
(In other words the number of columns required, like in this example, I would need
two Tab stops).
Step 2: Specify the value of the column where each Tab should start.
Step 3: The #9 must be used to move from one Tab stop to the other.
Compiled by Georgina Ramsamy
Example:
Step 1: I require two Tab stops.
redOutput.Paragraph.TabCount := 2;
Step 2: Specify the column value where each Tab should start.
redOutput.Paragraph.Tab[0] := 10;
redOutput.Paragraph.Tab[1] := 100;
Step 3: The #9 must be used to move from one Tab stop to the other.
redOutput.Lines.Add('Name: '+ #9+ sFirstname);
The display part of the program segment will now change to:
The output would look as follows:
Compiled by Georgina Ramsamy
Formatting Float / Real / Double values to two decimal places.
Example:
The variable rNumber1 is declared as real in the program segment.
In the program the value is set as follows:
rNumber1:= 713.6795678;
Format this value to round it off to two decimal places
Display the formatted value in an edit box.
The display will be as follows:
Notes:
FloatToStrF This function is used to convert a real number to a string with formatting
rNumber1 The variable name of the value to be converted
ffFixed Used when rounding off to a specific number of decimal places
5 The maximum number of digits the integer part of the number can contain
2 The number of digits after the decimal point
Formatting to currency
Example:
The variable rPrice is declared as real in the program segment.
In the program the value is set as follows:
rPrice := 927.89;
Format this value to currency and to two decimal places
Display the formatted value in an edit box.
Compiled by Georgina Ramsamy
Notes:
FloatToStrF This function is used to convert a real number to a string with formatting
rPrice The variable name of the value to be converted
ffCurrency Used to specify the currency and to round off to a specific number of
decimal places. The currency displayed will depend on the system
settings.
5 The maximum number of digits the integer part of the number can contain
2 The number of digits after the decimal point
The display will be as follows:
Compiled by Georgina Ramsamy