Delphi Tutorial

Learning Delphi may be seen as two separate activities:

  1. Learning how to use the controls and components that can be placed on a form, setting their properties and choosing the event that runs their code;
  2. Learning how to write program code (Object Pascal) and event handlers that will carry out the tasks required by the controls and components when they are activated.

A series of exercises accompanies these notes.

Programming in Pascal

Pascal is an imperative or algorithmic style language; Borland Object Pascal, the programming language used by Delphi, is an enhanced version of Pascal that includes object-oriented features.

Data Types

Some programming languages are flexible with their data types but Object Pascal is formal and strict. There are three main data types, numeric, character and Boolean...

Operators

The main operators are +, -, * and /. Integer division has two special operators, mod and div, which prove very useful in various mathematical problems (div is integer division, as in 7 div 2 = 3; mod gives the modulus or remainder, as in 7 mod 2 = 1).

Programming exercises 1-5.

Self-Review 1

Programming Constructs

Programming in an imperative language may be seen as having two main parts: programming constructs and data structures. There are three main structures...

Programming exercises 6-11.

Functions

A function is a structure in Pascal that is used to perform some calculation and to return a single value as a result. A number of functions are built into Pascal such as inc(), strtoint() and sqrt(). Other functions can be defined by the user to perform specific tasks such calculating powers or custom calculations. Putting code inside a function means that the calculation can be made from different parts of a program without having to write out the code separately each time. Example.

Procedures

Procedures provide a way of bundling code into a block that can be called from anywhere in a program. Procedures can return one or more results or none at all and are more flexible than functions. You will have noticed procedures in Delphi almost immediately because they are a central part of the way the language works. Each event that happens on a form is coded in a separate procedure so that it can run independently and does not interfere with other parts of the program.

Other Features of Pascal

Operations that may be contained inside the three main constructs include input and output, operations on files and data structures, graphics routines and commands relating to specific technologies such as multimedia and the internet.

Standard Controls

A control is an object, consisting of properties and methods, that can be placed on a form. A control is also a component that is visible on a form. A Timer is not visible and so is strictly called a component while an Edit is visible and is therefore called a control.

A good place to start programming in Delphi is with the Standard Palette, which contains many of the object used most frequently in Windows programming and which have been around for some time.

Label Edit Button ListBox

Programming exercises 12-17.

The Standard Controls palette includes controls that can improve the interface between programs and users.

ComboBox Memo MainMenu CheckBox
RadioButton RadioGroup GroupBox Panel
ActionList PopUpMenu ScrollBar Frame

Programming exercises 18-21.

Algorithms

Having seen how some of the standard controls work we can now consider some more difficult algorithmic problems.

Fibonacci sequence Prime numbers Digital River Mayan Calendar

Programming exercises 22-26.

Self-Review 2

Static Data Structures

Pascal provides three major data structures, the array, the record and the set. Pascal also provides pointers so that dynamic data structures can be created.

Data Structures and Algorithms
Denary-Binary Linear Search Decimal-Roman Reverse a List
ISBN Lojban Passwords Circle counting

Programming exercises 27-33.

Properties and Events

This section examines those properties that are common to many controls. It also examines the most common events that can be used to cause code to run.

Files

A key part of programming is that data entered into memory can be transferred to external storage so that they can be reloaded later or passed to another user.

Text Files Sequential Files Sequential Files 2 Direct Access Files

Forms and Messages

This section includes detailed aspects of forms and also different forms of message that can be generated.

Formatting Output

Output of numbers and strings can be formatted with the powerful format function.

Programming exercises 34-35.

Win32 Controls

As Windows has developed, more functionality has been added and the more recent controls are available from the Win32 palette. These are three of the most commonly used controls:

DateTimePicker RichEdit ImageList

Printing

There are various ways to produce printed output in Delphi.

Numbers and Dates

This section looks at numeric conversions and the use of dates and times.

Dialogs

Another important set of functions, some of which you may have encountered already in the sections above, is the Dialogs palette.

File Open File Save Open Picture Save Picture
Font Color Printer Printer Setup
Find Replace    

Other Win32 Controls

These are sophisticated and more likely to be used in A2 projects.

ListView TreeView StatusBar ToolBar
CoolBar HeaderControl ComboBoxEx PageControl

Additional Controls

Borland have added their own controls in Delphi to extend the range of facilities in Windows. Some of these are very useful in basic projects.

StringGrid Image Shape LabelledEdit

These controls require some effort to understand but may have some use in projects:

ControlBar ActionManager ColorBox MaskEdit

Other controls in this section are sophisticated but will require some work before they can be used in projects:

BitButton SpeedButton Bevel Splitter
ActionToolBar ActionMainMenu Chart ValueListEditor

System Controls

Some functions are dependent on system variables, one of the most useful being the system clock and timers. Another potentially useful component in the System palette is the OLEContainer.

Samples and ActiveX Controls

Some additional components and controls are available in the Samples and ActiveX palettes:

Gauge ColorGrid SpinButton
SpinEdit DirectoryOutline Calendar
ShellTreeView ShellComboBox ShellListView

Database Controls

This sequence of guides shows you how to use ADO databases with Delphi:

Create Access Database Databases in Delphi 1
Library Database Music Database
Advanced Topics Rave Reports (Delphi 2005)
(Database Example: Invoices) (Quick Reports Invoices)
(Invoices Part 2) (Invoices: Search & Report)

Database access features are in the DataAccess and DataControls palettes:

Data Source    
DB Grid DB Navigator Other Controls

ADO-specific controls and components are in the ADO palette:

ADOConnection ADOCommand ADODataSet
ADOTable ADOQuery ADO Methods

Advanced Algorithms

Binary Search Bubble Sort Insertion sort
Noughts & Crosses Hangman  

Recursion

The A2 Computing syllabus requires some familiarity with the technique of recursion. This is where a procedure or function calls itself in the manner of iteration. Changes in the value of parameters with each call give the technique much of its power.

Factorials GCD Towers of Hanoi Quick Sort

Algorithms and Dynamic Data Structures

The A2 Computing syllabus requires some familiarity with data structures that are not fixed in size and can expand and contract during execution. Some aspects of dynamic data structures have been replaced by objects (see object oriented programming below).

Linked Lists Stacks Queues
Trees    

Object Oriented Programming (OOP)

You have been using an object-oriented programming environment but so far you have used existing classes and objects provided by the Delphi programming environment. It is time now to consider creating your own classes and objects and to develop your knowledge of object oriented programming.

OOP OOP Stack OOP Queue
Stacks: Reverse Polish    

Back to Delphi Home Page