Delphi Components & Events Grade 10
Beyond buttons and labels — these components are used in most IT practical exams. Know each one's key property and how to use it in code.
Input Components
| Component | Key Property/Event | How to use in code |
|---|---|---|
| TEdit | .Text | edtName.Text — read or write as string |
| TSpinEdit (sedNum) | .Value | sedNum.Value — returns Integer directly |
| TComboBox | .Text or .ItemIndex | cboMonth.Text or cboMonth.ItemIndex (0-based) |
| TListBox | .ItemIndex, .Items | lstNames.Items.Add('Alice'); lstNames.ItemIndex |
| TRadioGroup | .ItemIndex | if rgpGender.ItemIndex = 0 then (0 = first item) |
| TCheckBox | .Checked | if chkAgree.Checked then |
| TTrackBar | .Position | trkVolume.Position — Integer |
| TDateTimePicker | .Date | dtpBirth.Date — TDate value |
Output Components
| Component | Key Property | Code example |
|---|---|---|
| TLabel | .Caption | lblResult.Caption := 'Done'; |
| TMemo | .Lines | memOut.Lines.Add('Hello'); |
| TRichEdit | .Lines | Same as Memo; supports formatting |
| TListBox | .Items | lstOut.Items.Add('Alice'); |
| TImage | .Picture | imgPhoto.Picture.LoadFromFile('face.jpg'); |
| TProgressBar | .Position | pbrLoad.Position := 75; |
| TPanel | .Caption, .Color | pnlInfo.Caption := 'Ready'; |
PageControl (Tabbed Forms)
A TPageControl creates tabbed pages on a form. Each tab is a TTabSheet.
Creating tabs
- Add
TPageControlto the form - Right-click → New Page to add tabs
- Set each
TTabSheet.Captionin the Object Inspector - Drop other components onto each tab — they belong to that tab
Controlling tabs in code
// Show a tab
TabSheet2.TabVisible := True;
// Hide a tab
TabSheet2.TabVisible := False;
// Jump to a specific tab
pgcMain.ActivePage := TabSheet3;Timer Component
A TTimer fires its OnTimer event at regular intervals without user input.
| Property | Meaning |
|---|---|
Interval | Milliseconds between triggers (1000 = 1 second) |
Enabled | True/False — starts or stops the timer |
var
iSeconds: Integer; // global variable
// OnTimer event (fires every 1000ms)
procedure TForm1.tmrCountTimer(Sender: TObject);
begin
Dec(iSeconds);
lblTime.Caption := IntToStr(iSeconds) + ' seconds left';
if iSeconds <= 0 then
begin
tmrCount.Enabled := False;
ShowMessage('Time is up!');
end;
end;ComboBox — Adding Items
// At design time: use Items property in Object Inspector
// At runtime:
cboMonth.Items.Add('January');
cboMonth.Items.Add('February');
cboMonth.ItemIndex := 0; // select first item
// Reading selected item
sSelected := cboMonth.Text;
iIndex := cboMonth.ItemIndex;ListBox — Adding, Clearing, Accessing Items
lstNames.Items.Add('Alice'); // add item
lstNames.Items.Clear; // remove all
lstNames.Items.Delete(0); // remove first item
iCount := lstNames.Items.Count; // number of items
sItem := lstNames.Items[2]; // access item at index 2
sSelected := lstNames.Items[lstNames.ItemIndex]; // selected itemResetting a Selection — the -1 Trick
The ItemIndex property tells you which item is selected, counting from 0 (the first item). When nothing is selected, ItemIndex is -1.
This works in reverse too: setting ItemIndex := -1 deselects everything and visually clears the box. This is the standard way to reset a TComboBox, TListBox or TRadioGroup — for example when a user clicks a "Clear form" button.
cboMonth.ItemIndex := -1; // ComboBox: no month selected (box appears empty)
rgpGender.ItemIndex := -1; // RadioGroup: no radio button filled in
lstNames.ItemIndex := -1; // ListBox: nothing highlightedIf the user has not chosen anything, ItemIndex is -1. Reading cboMonth.Items[cboMonth.ItemIndex] while it is -1 causes a runtime error (list index out of bounds). Guard it first:
if cboMonth.ItemIndex = -1 then
ShowMessage('Please select a month first.')
else
sMonth := cboMonth.Text;Naming Convention for Components
| Prefix | Component |
|---|---|
btn | TButton |
lbl | TLabel |
edt | TEdit |
mem | TMemo |
sed | TSpinEdit |
cbo | TComboBox |
lst | TListBox |
rgp | TRadioGroup |
chk | TCheckBox |
pgc | TPageControl |
tmr | TTimer |
img | TImage |