Introduction to Delphi Grade 10
Delphi is a visual programming environment — you design the screen AND write the code. You drag components onto a form to build the interface, then write Pascal code to make it all work. This page covers the IDE layout, components, naming, events, and your first program.
The Delphi IDE — What You See When You Open It
When you open Delphi, this is what you will see. It might look overwhelming at first — but each area has one clear job:
Switching to Code View
Press F12 to toggle between the Design View (what you see above) and the Code View (where you type your Delphi code).
The Delphi IDE Layout
The Integrated Development Environment (IDE) is where you write, design and run your programs. Think of it as your workshop — every tool you need is right there.
Key IDE Panels
| Panel | Purpose |
|---|---|
| Menu Bar | Top menu — File, Edit, View, Project, Run, Tools etc. |
| Tool Palette | All Delphi components grouped by category. Click a component, then click on the form to place it. |
| Object Inspector | Sets the initial properties and events of a selected component. |
| Structure Panel | Quick reference showing all objects on the current form. |
| Form / Design Area | The visual canvas — drag and drop components here to build the UI. |
| Code View | Where you write Pascal code. Toggle with F12. |
Step-by-Step: Creating Your First Project
- File → New → Windows VCL Application — creates a blank Form (TForm).
- Place components by clicking them in the Tool Palette, then clicking on the form.
- Set properties in the Object Inspector (e.g. change a button's Caption).
- Double-click a button to go to Code View and write the event handler.
- File → Save All (Ctrl+Shift+S) — save in a NEW dedicated folder.
- Press F9 to compile and run.
Each project must be saved in its own dedicated folder. Save the Unit file as FileName_u.pas and the Project file as FileName_p.dproj. Never mix two projects in one folder.
Visual Component Reference
Components are the building blocks placed on a form. Each is an object with properties and events. Here is what the most common ones look like:
Naming Convention
Every component gets a prefix that indicates its type, followed by a meaningful name. This makes code readable.
| Prefix | Component Type | Example |
|---|---|---|
btn | TButton | btnCalculate |
lbl | TLabel | lblResult |
edt | TEdit | edtName |
mem | TMemo | memOutput |
img | TImage | imgLogo |
pnl | TPanel | pnlHeader |
sed | TScrolledEdit | sedNotes |
cbo | TComboBox | cboGrade |
lst | TListBox | lstNames |
rgp | TRadioGroup | rgpGender |
chk | TCheckBox | chkAgree |
tmr | TTimer | tmrClock |
Event-Driven Programming
Delphi programs are event-driven — code only runs when something HAPPENS (an event), like pressing a button, typing in a box, or a timer ticking.
Think of a vending machine. It just sits there doing nothing — until you push a button. That press is the event. The machine then dispenses the item — that is the event handler (your code). If no button is pressed, nothing happens. Delphi works exactly like this.
| Event | When it fires | Common use |
|---|---|---|
OnClick | User clicks a button / component | Calculate, submit, navigate |
OnChange | Text in an edit box changes | Live validation, update display |
OnKeyPress | A key is pressed while component has focus | Accept only numbers |
OnCreate | Form loads / opens | Initialise variables, load data |
OnTimer | Timer interval elapses | Countdown, clock display |
How to Create an Event Handler
- In Design View, double-click a button to create its
OnClickhandler automatically. - For other events, go to the Events tab in the Object Inspector and double-click next to the event name.
- Delphi creates a skeleton procedure. Write your code between
beginandend. - Never change the procedure header — only add code inside.
procedure TForm1.btnCalculateClick(Sender: TObject);
begin
// Your code goes here
ShowMessage('Button clicked!');
end;
ShowMessage and InputBox
ShowMessage displays a pop-up with a message. InputBox pops up a window asking the user to type something. Both are modal — the program pauses until the user responds.
ShowMessage('Hello world!');
ShowMessage('Result: ' + IntToStr(iAnswer));
// Returns a string
sName := InputBox('Name', 'Enter your name:', '');
// Convert to integer
iNum := StrToInt(InputBox('Number', 'Enter a number:', ''));
// Convert to real
rAmount := StrToFloat(InputBox('Amount', 'Enter amount:', ''));
// Get first character only
cClass := InputBox('Class', 'Enter class:', '')[1];
Components — Properties
Properties control how a component looks and behaves. Set them in the Object Inspector or through code.
| Property | Description | Example Value |
|---|---|---|
Name | Unique identifier used in code | btnCalculate |
Caption | Text shown on labels, buttons, forms | 'Calculate' |
Text | Text in a TEdit box | edtName.Text |
Height | Component height in pixels | 30 |
Width | Component width in pixels | 150 |
Font.Size | Text size | 14 |
Font.Color | Text colour (note: Color not Colour) | clRed |
Visible | Whether component is shown | True / False |
Setting Properties in Code
Format: ComponentName.Property := Value;
Form1.Height := 500;
Button1.Caption := 'Register';
Label1.Font.Color := clGreen;
Shape1.Brush.Color := clRed;
Shape1.Top := 20;
Shape1.Left := 20;
Methods
Methods are pre-written operations that perform tasks on components. Format: ComponentName.Method;
| Method | Effect |
|---|---|
Button1.Hide; | Makes the button invisible |
Button1.Show; | Makes the button visible again |
Edit1.SetFocus; | Moves the cursor into the edit box |
Memo1.Clear; | Clears all text in the memo |
Form1.Close; | Closes the form / program |
RichEdit1.Lines.Add('text'); | Adds a new line to a RichEdit or Memo |
Syntax Rules
Every statement ends with a semicolon ;. Missing one causes a syntax error. There is never a semicolon before else. Begin/end pairs must always match.