IT Grade 10 - 12 Delphi Elements
IT Grade 10 - 12 Delphi Elements
1. ShowMessage('Message') (Method)
o Description: Displays a simple dialog box with a specified message.
o How it Works: The ShowMessage method creates a modal dialog box that shows the
provided message and an OK button. The dialog must be dismissed before the program
continues.
o Example:
ShowMessage('Hello, World!');
o Result: An input box appears with the title "Enter Name", a prompt "Please enter your
name:", and "John Doe" as the default text.
o Result: An information dialog box appears with the message "This is an information
dialog." and an OK button.
Page 1 of 38
IT Grade 10 – 12 Delphi Elements
var x: Integer;
x := 15;
if x > 10 then
ShowMessage('x is greater than 10')
else
ShowMessage('x is not greater than 10');
o Result: A message box appears with the text "x is greater than 10".
Page 2 of 38
IT Grade 10 – 12 Delphi Elements
var i: Integer;
for i := 1 to 5 do
ShowMessage('Count: ' + IntToStr(i));
o Result: Five message boxes appear in sequence, displaying "Count: 1", "Count: 2", and
so on up to "Count: 5".
var x: Integer;
x := 1;
while x <= 3 do
begin
ShowMessage('Value: ' + IntToStr(x));
Inc(x);
end;
o Result: Three message boxes appear in sequence, displaying "Value: 1", "Value: 2", and
"Value: 3".
Page 3 of 38
IT Grade 10 – 12 Delphi Elements
var x: Integer;
x := 1;
repeat
ShowMessage('Value: ' + IntToStr(x));
Inc(x);
until x > 3;
o Result: Three message boxes appear in sequence, displaying "Value: 1", "Value: 2", and
"Value: 3".
9. Length(Str) (Method)
o Description: Returns the length of a string.
o How it Works: The Length function counts the number of characters in a string and
returns this value as an integer.
o Example:
o Result: A message box appears with the text "Length of string: 5".
Page 4 of 38
IT Grade 10 – 12 Delphi Elements
subStr := Copy('Hello', 2, 3);
ShowMessage(subStr);
Page 5 of 38
IT Grade 10 – 12 Delphi Elements
o Result: A message box appears with the text "Position of 'l': 3".
Page 6 of 38
IT Grade 10 – 12 Delphi Elements
Page 7 of 38
IT Grade 10 – 12 Delphi Elements
o Result: A message box appears with the text "String: 123.45".
o Result: A message box appears with the text "Square root: 5".
Page 8 of 38
IT Grade 10 – 12 Delphi Elements
ShowMessage('Absolute value: ' + IntToStr(absVal));
o Result: A message box appears with the text "Absolute value: 10".
Randomize;
o Result: A message box appears with a random number between 0 and 99.
Page 9 of 38
IT Grade 10 – 12 Delphi Elements
rounded := Round(5.5);
ShowMessage('Rounded: ' + IntToStr(rounded));
var x: Integer;
x := 10;
Inc(x);
ShowMessage('Incremented value: ' + IntToStr(x));
o Result: A message box appears with the text "Incremented value: 11".
var x: Integer;
Page 10 of 38
IT Grade 10 – 12 Delphi Elements
x := 10;
Dec(x);
ShowMessage('Decremented value: ' + IntToStr(x));
o Result: A message box appears with the text "Decremented value: 9".
o Result: An input dialog appears. If the user enters a name, a message box appears with
"Hello, [name]".
File Handling
var f: TextFile;
AssignFile(f, 'data.txt');
Page 11 of 38
IT Grade 10 – 12 Delphi Elements
31. Reset(File) (Method)
o Description: Opens a text file for reading.
o How it Works: The Reset procedure opens the file associated with File for reading. If
the file does not exist, an error occurs.
o Example:
var f: TextFile;
AssignFile(f, 'data.txt');
Reset(f);
var f: TextFile;
AssignFile(f, 'data.txt');
Rewrite(f);
o Result: The file 'data.txt' is opened for writing, and its contents are cleared.
var f: TextFile;
AssignFile(f, 'data.txt');
Append(f);
o Result: The file 'data.txt' is opened for writing, and new content will be added to the end
of the file.
Page 12 of 38
IT Grade 10 – 12 Delphi Elements
o How it Works: The CloseFile procedure closes the file associated with File, finalizing
any operations and freeing system resources.
o Example:
var f: TextFile;
AssignFile(f, 'data.txt');
CloseFile(f);
var f: TextFile;
var line: String;
AssignFile(f, 'data.txt');
Reset(f);
ReadLn(f, line);
ShowMessage(line);
CloseFile(f);
var f: TextFile;
AssignFile(f, 'data.txt');
Rewrite(f);
Page 13 of 38
IT Grade 10 – 12 Delphi Elements
WriteLn(f, 'Hello, World!');
CloseFile(f);
var f: TextFile;
var line: String;
AssignFile(f, 'data.txt');
Reset(f);
while not Eof(f) do
begin
ReadLn(f, line);
ShowMessage(line);
end;
CloseFile(f);
o Result: Each line in 'data.txt' is displayed in a message box until the end of the file is
reached.
Operators
Page 14 of 38
IT Grade 10 – 12 Delphi Elements
o How it Works: The Div operator performs division between two integers and returns the
integer quotient, ignoring the remainder.
o Example:
o Result: A message box appears with the text "10 div 3 = 3".
o Result: A message box appears with the text "10 mod 3 = 1".
40. + (Operator)
o Description: Adds two numbers.
o How it Works: The + operator adds two numerical values and returns the sum.
o Example:
Page 15 of 38
IT Grade 10 – 12 Delphi Elements
41. - (Operator)
o Description: Subtracts one number from another.
o How it Works: The - operator subtracts the second number from the first and returns the
result.
o Example:
42. * (Operator)
o Description: Multiplies two numbers.
o How it Works: The * operator multiplies two numerical values and returns the product.
o Example:
43. / (Operator)
o Description: Divides one number by another.
o How it Works: The / operator divides the first number by the second and returns the
result as a floating-point number.
o Example:
Page 16 of 38
IT Grade 10 – 12 Delphi Elements
o Result: A message box appears with the text "5 / 3 = 1.66666666666667".
44. := (Operator)
o Description: Assigns a value to a variable.
o How it Works: The := operator assigns the value on the right side to the variable on the
left side.
o Example:
var x: Integer;
x := 10;
ShowMessage('x = ' + IntToStr(x));
45. = (Operator)
o Description: Checks if two values are equal.
o How it Works: The = operator compares two values and returns True if they are equal,
False otherwise.
o Example:
o Result: A message box appears with the text "5 equals 5".
Page 17 of 38
IT Grade 10 – 12 Delphi Elements
isNotEqual := (5 <> 3);
if isNotEqual then
ShowMessage('5 does not equal 3');
o Result: A message box appears with the text "5 does not equal 3".
o Result: A message box appears with the text "3 is less than 5".
o Result: A message box appears with the text "5 is greater than 3".
Page 18 of 38
IT Grade 10 – 12 Delphi Elements
var isLessOrEqual: Boolean;
isLessOrEqual := (3 <= 5);
if isLessOrEqual then
ShowMessage('3 is less than or equal to 5');
o Result: A message box appears with the text "3 is less than or equal to 5".
o Result: A message box appears with the text "5 is greater than or equal to 3".
Events
o Result: When the button is clicked, a message box appears with the text "Button
clicked!".
Page 19 of 38
IT Grade 10 – 12 Delphi Elements
52. OnCreate (Event)
o Description: Triggered when a form is created, often used to initialize variables or
components.
o How it Works: The OnCreate event is triggered as soon as the form is created, allowing
you to set initial values or perform startup tasks.
o Example:
o Result: When the form is created, a message box appears with the text "Form created!".
o Result: When the form is shown, a message box appears with the text "Form shown!".
o Result: When the form is about to close, a message box appears with the text "Form
closing!".
Page 20 of 38
IT Grade 10 – 12 Delphi Elements
55. OnResize (Event)
o Description: Triggered when a form or component is resized.
o How it Works: The OnResize event occurs whenever the form or a component within it
is resized. This can be used to adjust the layout dynamically.
o Example:
o Result: When the form is resized, a message box appears with the text "Form resized!".
Page 21 of 38
IT Grade 10 – 12 Delphi Elements
o How it Works: The IsLeapYear function checks if the given year meets the criteria for a
leap year (divisible by 4, but not by 100 unless also divisible by 400).
o Example:
o Result: A message box appears with the text "2024 is a leap year".
o Result: A message box appears with the text "Days in February 2024: 29".
o Result: A message box appears with the text "Encoded Date: 29/02/2024".
Page 22 of 38
IT Grade 10 – 12 Delphi Elements
61. DecodeDate(Date, Year, Month, Day) (Method)
o Description: Decodes a TDateTime value into individual year, month, and day
components.
o How it Works: The DecodeDate procedure extracts the year, month, and day from a
TDateTime value and stores them in the respective variables.
o Example:
o Result: A message box appears with the text "Year: 2023, Month: 12, Day: 31".
o Result: A message box appears with the text "Number: 123" if the conversion is
successful.
o Result: A message box appears with the text "Rounded to 2 decimal places: 123.46".
Page 23 of 38
IT Grade 10 – 12 Delphi Elements
64. In (Operator)
o Description: Checks if a value is within a specific set.
o How it Works: The In operator is used to determine if a value is included within a
specified set. It returns True if the value is found in the set, otherwise False.
o Example:
var i: Integer;
for i := 1 to 10 do
begin
if i = 5 then
Break;
ShowMessage('Count: ' + IntToStr(i));
end;
Result: Message boxes appear with "Count: 1", "Count: 2", "Count: 3", "Count: 4", then
o
the loop exits.
66. Continue (Control Structure)
o Description: Skips the current iteration of a loop and continues with the next iteration.
o How it Works: The Continue statement skips the remaining code in the current iteration
of the loop and jumps to the next iteration.
o Example:
var i: Integer;
for i := 1 to 5 do
begin
if i = 3 then
Continue;
ShowMessage('Count: ' + IntToStr(i));
end;
Page 24 of 38
IT Grade 10 – 12 Delphi Elements
o Result: Message boxes appear with "Count: 1", "Count: 2", "Count: 4", "Count: 5". The
message for "Count: 3" is skipped.
try
ShowMessage('Trying...');
finally
ShowMessage('Finally executed');
end;
try
var result := 10 div 0;
ShowMessage('Result: ' + IntToStr(result));
except
Page 25 of 38
IT Grade 10 – 12 Delphi Elements
on E: EDivByZero do
ShowMessage('Error: Division by zero!');
end;
o Result: A division by zero error triggers the except block, displaying a message box
with the text "Error: Division by zero!".
o Result: A message box appears with the text "Currency as string: 1234.56".
Components
Page 26 of 38
IT Grade 10 – 12 Delphi Elements
o Result: The label on the form displays the text "Enter your name:".
Button1.Caption := 'Submit';
Button1.OnClick := Button1Click;
o Result: The button displays the text "Submit" and triggers the Button1Click event when
clicked.
o Result: The edit box displays the default text "Enter your text here", which the user can
modify.
o Result: The memo box displays the line "This is a memo component."
Page 27 of 38
IT Grade 10 – 12 Delphi Elements
ComboBox1.Items.Add('Option 1');
ComboBox1.Items.Add('Option 2');
ComboBox1.ItemIndex := 0;
o Result: Adds two options to the combo box and selects the first option by default.
ListBox1.Items.Add('Item 1');
ListBox1.Items.Add('Item 2');
o Result: The list box displays "Item 1" and "Item 2".
Page 28 of 38
IT Grade 10 – 12 Delphi Elements
o Result: The checkbox displays with the text "I agree to the terms" and is checked by
default.
o Result: Displays two radio buttons with "Option 1" selected by default.
Page 29 of 38
IT Grade 10 – 12 Delphi Elements
o Result: The timer triggers every 1 second when enabled.
o Result: The button displays the caption "Click Me" along with an image loaded from
"icon.bmp".
Events
o Result: When the text in the edit box changes, a message box appears with the text "Text
changed!".
Page 30 of 38
IT Grade 10 – 12 Delphi Elements
begin
ShowMessage('Timer tick!');
end;
o Result: A message box appears with the text "Timer tick!" at regular intervals based on
the TTimer component settings.
o Result: The query retrieves all records from the "Students" table in the connected
database.
ADOConnection1.ConnectionString := 'Provider=SQLOLEDB;Data
Source=MyServer;Initial Catalog=MyDatabase;User
Id=myUsername;Password=myPassword;';
ADOConnection1.Open;
o Result: Establishes a connection to the specified database using the provided connection
string.
Page 31 of 38
IT Grade 10 – 12 Delphi Elements
o Example:
DataSource1.DataSet := ADOQuery1;
o Result: Links the ADOQuery1 dataset to the DataSource1 component, allowing data-
aware controls to access the query results.
DBGrid1.DataSource := DataSource1;
o Result: The data from DataSource1 (and therefore ADOQuery1) is displayed in DBGrid1.
o Result: Iterates through the records in the ADOQuery1 dataset, displaying the
"StudentName" field for each record until the end of the dataset is reached.
Page 32 of 38
IT Grade 10 – 12 Delphi Elements
o How it Works: TClientDataSet is an in-memory dataset that can store data locally and
manipulate it without a persistent connection to the database.
o Example:
ClientDataSet1.LoadFromFile('Students.xml');
ClientDataSet1.Open;
o Result: Loads data from an XML file into the ClientDataSet1 component and opens
the dataset for use.
ClientDataSet1.SaveToFile('Backup.xml');
o Result: Saves the current data in ClientDataSet1 to an XML file named "Backup.xml".
o Result: Retrieves the value of the "StudentName" field from the current record and
displays it in a message box.
Page 33 of 38
IT Grade 10 – 12 Delphi Elements
o Description: Searches for a record in a dataset that matches a specified condition.
o How it Works: The Locate method searches the dataset for a record that matches the
specified criteria. If found, the record pointer is moved to that record.
o Example:
o Result: If a record with StudentID = 12345 is found, the student's name is displayed in
a message box.
o Result: Adds a new item to ListView1 with the caption "Student 1" and a subitem
"Grade 10".
ComboBox1.Items.Add('Option 1');
ComboBox1.Items.Add('Option 2');
ComboBox1.ItemIndex := 0;
o Result: Adds two options to ComboBox1 and selects the first option by default.
Page 34 of 38
IT Grade 10 – 12 Delphi Elements
StringGrid1.Cells[0, 0] := 'Name';
StringGrid1.Cells[1, 0] := 'Grade';
StringGrid1.Cells[0, 1] := 'John Doe';
StringGrid1.Cells[1, 1] := 'A';
o Result: Populates StringGrid1 with headers "Name" and "Grade" in the first row, and
the student "John Doe" with a grade of "A" in the second row.
o Result: Finds all .txt files in the current directory and displays each file name in a
message box.
Page 35 of 38
IT Grade 10 – 12 Delphi Elements
o Result: The timer triggers every 1 second when enabled.
o Result: The button displays the caption "Click Me" along with an image loaded from
"icon.bmp".
o Result: The checkbox displays with the text "I agree" and is checked by default.
o Result: Creates a tabbed interface with two tabs, "Tab 1" and "Tab 2", and makes "Tab
1" the active tab.
Page 36 of 38
IT Grade 10 – 12 Delphi Elements
Events
o Result: Filters the dataset to only include records where the "Grade" field is "A".
Page 37 of 38
IT Grade 10 – 12 Delphi Elements
begin
ADOQuery1.SQL.Text := 'SELECT * FROM Students WHERE Grade = ''A''';
end;
o Result: Modifies the SQL query to only select students with a grade of "A" before the
query is executed.
104. OnAfterPost (Event - TClientDataSet)
o Description: Triggered after changes are posted to a dataset, allowing for further
processing.
o How it Works: The OnAfterPost event is used to perform actions after a record has
been posted to the dataset, such as displaying a confirmation message or updating related
data.
o Example:
Page 38 of 38