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

ComponentKey Property/EventHow to use in code
TEdit.TextedtName.Text — read or write as string
TSpinEdit (sedNum).ValuesedNum.Value — returns Integer directly
TComboBox.Text or .ItemIndexcboMonth.Text or cboMonth.ItemIndex (0-based)
TListBox.ItemIndex, .ItemslstNames.Items.Add('Alice'); lstNames.ItemIndex
TRadioGroup.ItemIndexif rgpGender.ItemIndex = 0 then (0 = first item)
TCheckBox.Checkedif chkAgree.Checked then
TTrackBar.PositiontrkVolume.Position — Integer
TDateTimePicker.DatedtpBirth.Date — TDate value

Output Components

ComponentKey PropertyCode example
TLabel.CaptionlblResult.Caption := 'Done';
TMemo.LinesmemOut.Lines.Add('Hello');
TRichEdit.LinesSame as Memo; supports formatting
TListBox.ItemslstOut.Items.Add('Alice');
TImage.PictureimgPhoto.Picture.LoadFromFile('face.jpg');
TProgressBar.PositionpbrLoad.Position := 75;
TPanel.Caption, .ColorpnlInfo.Caption := 'Ready';

PageControl (Tabbed Forms)

A TPageControl creates tabbed pages on a form. Each tab is a TTabSheet.

Creating tabs

  1. Add TPageControl to the form
  2. Right-click → New Page to add tabs
  3. Set each TTabSheet.Caption in the Object Inspector
  4. Drop other components onto each tab — they belong to that tab

Controlling tabs in code

Delphi
// 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.

PropertyMeaning
IntervalMilliseconds between triggers (1000 = 1 second)
EnabledTrue/False — starts or stops the timer
Delphi — countdown 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

Delphi
// 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

Delphi
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 item

Resetting 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.

Delphi — clearing selections
cboMonth.ItemIndex  := -1;   // ComboBox: no month selected (box appears empty)
rgpGender.ItemIndex := -1;   // RadioGroup: no radio button filled in
lstNames.ItemIndex  := -1;   // ListBox: nothing highlighted
Always check for -1 before reading a selection

If 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:

Delphi — safe reading
if cboMonth.ItemIndex = -1 then
  ShowMessage('Please select a month first.')
else
  sMonth := cboMonth.Text;

Naming Convention for Components

PrefixComponent
btnTButton
lblTLabel
edtTEdit
memTMemo
sedTSpinEdit
cboTComboBox
lstTListBox
rgpTRadioGroup
chkTCheckBox
pgcTPageControl
tmrTTimer
imgTImage