Term 1 Delphi Notes Jassem Saoud
Term 1 Delphi Notes Jassem Saoud
Component Prefix
Form frm
Label lbl
Button btn
● When trying to quickly pull up a variable type the prefix of the variable name then press CTRL +
SPACE then double click the variable you'd like to insert it
● When an object/button is set to enabled false the user is able to see it but can’t click on it
● Setting a Bitmap button to Kind = bkClose sets the button to close the interface when clicked
● You can change the entire font of your form if you go into properties and search for font
● To change the font click on the ellipsis by the font setting
● The prefix shp stand for shape
● When naming projects use _U at the end for a unit file and _P at the end for program files
● A program file contains all of the unit files
● To make a line of code a comment press CTRL + Forward Slash
● To hide an image, type imgName.Visible:= False;
● To show an image, type imgName.Visible:= True;
● To make something a fast key, type & in front of the name of the element
● To change an Image’s picture to something from your saved folder, type
imgName.Picture.Loadfromfile(‘File’)
● When loading an image from your folder remember that you have to copy the image into your
debug file first
● True or False statements are boolean statements
● Boolean statements answers are yes or no/ true or false
● String integers are integers which values can be numbers or letters and are specific
● You can use methods to perform pre-programmed tasks
● To use a method type ComponentName.MethodName;
● SetFocus is an example of a method
● Clear is an example of a method
● When using a BitButton to change it to a Retry button, go into its Kind Property and change the
last part to Retry
● To code a memo to show a string, type memName.Lines.Add(‘string’);
● To delete a line on a memo, go into the lines property and backspace what you'd like to delete
● To delete a line from the memo using code, type memDisplay.Clear; before adding any other
lines of code
● IntToStr converts an integer value to a string value
● FloatToStr converts a real value to a string value
● Use an onActivate event to code something so it occurs when you start your program
● Use an OnMouseMove event to code something so it occurs when you drag your mouse over a
component
● Use an OnContextPopUp event to code something so it occurs when you right click a
component
● Use an OnDblClick event to code something so it occurs when you double click a component
● Use an OnEnter event to code something so it occurs when you enter/use a component
● Use an OnExit event to code something so it occurs when you exit/stop using a component
● Rules for variable names:
● The name has to start with a letter or underscore
● The name may not contain spaces
● Letters, numbers and the underscore are allowed after the first character
● No special character can be used besides the underscore
● A name may not be a key word or a reserved word(word used by delphi and
already has a specific meaning e.g begin, var, end)
● Only the first 255 characters of the name will be considered as meaningful
● Multi-word variable names are probably the most effective in describing the
contents of a variable
● When writing multi-word variable names, remember to start each word with a
capital
● When dividing an integer, you can’t use /, you can only use DIV and MOD or you get a syntax
error
● When dividing a real, you can’t use DIV and Mod, only /
● Inc is a function, it stands for increment and can only be used for integer variables
● Inc(iVar,5) is an example which will add increments of 5 each time
● The default value of an Inc function is one
● Dec is a function that incrementally removes a number from an integer variable
● Dec(iVar,5) is an example which will remove increments of 5 each time
● The default value of a Dec function is one]
● Sed is the prefix for a spin edit, that is the edit object which only accepts numbers and can be
increased or decreased by pressing the up and down buttons on the edit
● If you want to format a FloatToStr statement, you would write FloatToStrF(varName, Format,
Precision, Decimal)
● The precision is the amount of bits you are allocating to the computer to allow it to calculate
the decimals. A good number to use is 15
● The decimal is the last decimal value you want to display for example in Pi, if you set decimal
as 4 it would display 3,1415
● There are many different formats but the main ones are ffCurrency and ffFixed
● Use a rich component rather than a memo component as it has more formatting options
● If you would like to insert a tab, write #9
● Objects in general have attributes and behaviors
● Delphi components are considered objects and each component has unique attributes and
behaviors
● The ceil function rounds the number up regardless of how small the decimal is as long as
there's a decimal
● The floor function rounds the number up regardless of how small the decimal is as long as
there's a decimal
● The trunk function does the same as the floor function
● The input box function pulls up a box which allows the user input information into it
● To display an input box use the code: sVariable := InputBox(‘Your Label’, ‘Prompt String’,
‘Default value’);
● To change the input box value into an integer or float use StrToInt/StrToFloat(InputBox());
● When declaring constants use CONST instead of VAR
● A constants value remains the same whilst the value of a variable changes
● To declare a constant its the same as declaring a variable
➔ Const
➔ VAT = 0.15;
● Constants don’t need a prefix when being declared
● Functions aren't able to stand alone and return data. E.g: StrToInt(), sVariable:= InputBox();
● Procedures are able to stand alone and don't return data. E.g: Showmessage:= ‘’, Itotal:=10,
Inc(iTotal,2)
● Pos function is used to find the position of a certain character found within a string. E.g:
iPos := Pos(‘o’, sString); will be equal to 4 if sString:= ‘I love programming’
● Pos functions always return an integer value
● iPos:= Pos(‘Program’,sString); will find the position of the first letter of program in the
string which in this case is 8
● Length function determines the length of a string and is used by saying iL :=
Length(sString);
● Length functions always return an integer value
● iPosSpace is used to find the first space within a string and is used by saying iPosSpace:=
Pos(‘’, sString);
● The copy function will copy a certain number of characters found within a string and returns
a string value. It's used by typing sWord:= Copy(sString,3,4); which would copy from the 3rd
character and 3 addition characters totalling in 4
● Alternatively, you could type sWord:= Copy(sString,iPosSpace+1,4); to copy the 4 characters
after the first space which in this case is love
● The Delete function is used to delete a certain number of characters from a string. You use it
by typing sWord: Delete(Variable, Position from where its deleting, Number of characters to
delete);
● The Insert function adds characters to a string
● You format an Insert like this → Insert( ‘What you want to insert’ , variable
you’re inserting into, position from where you’re inserting);
● tDate is a data type that is used to store the date of when the program is being launched
● DateToStr(Date)) returns the current date of the system as a string
● To extract the year from date to string, use copy(sDate,7,4);
● edtName.Text[1] displays only the first character of the edit you are able to change the
number in the square brackets to change the amount of characters shown
● The basic structure of an if statement is:
● If CONDITION then
begin
~~~~~~~~~
end
else/ else if
begin
~~~~~
end;
● When ending off an if statement, you should only put the semicolon after the ‘end’ function
of the last if/ else if statement
● When writing if statements, > means greater than, < means smaller than, = means equal and >=
or <= means greater than or equal/smaller than or equal
● Random function will randomly generate a number from set parameters you provide it
● Delphi always starts counting at 0 so 9 is the tenth number, so if you'd like to generate from
1-10 use + 1
● An example of a random function is: Randomize; iRandom := Random(10); which will generate
a random number from 0-9
● Randomize; iRandom := Random(10) +1; will generate a random number from 1-10
● Case statements can only check integer values and can only check a single character at a
time so they can't replace if statements
● The standard form for a case statement is:
● Case condition of
~~~~~
~~~~~
~~~~~
~~~~~
end;
● In a combo box, to add more options search for the items property
● Combo boxes return string values
● In combo boxes, if you would like to find the value of an item type: sVar:= cmbxColor.Item[1].
This will return the value of the second item
● In combo boxes, to determine which item was selected, type: iVar:= cmbxColor.ItemsIndex;
(THIS ALWAYS RETURNS AN INTEGER VALUE)
● If you would like to find the value of the selected item in a combo box, type: sVar:=
cmbxColor.Item[cmbColor.ItemsIndex];
● Another way to find the value of the selected item in a combo box is to type:
sVar:=cmbxColor.Text; This will return the value of the selected item, but if nothing is selected it
will return the default value(caption of the combo box)
● To add an item to a combo box, type: cmbxColor.Item.Add(‘x’); This will add an item at the
very end
● If you would like to add an item to a combo box in a specific position, type:
cmbxColor.Item.Insert(position, ‘x’);
● If you would like to find the position of a certain value in a combo box, type:
cmbxColor.Items.IndexOf(‘Item you're looking for’);
● To see how many items are in the combo box, type: cmbxColor.Items.Count;(REMEMBER,
THIS WILL START AT 0 SO IF THERE ARE 7 ITEMS THE VALUE IT RETURNS WILL BE 6)
● To delete a specific item from a combo box, type: cmbxColor.Items.Delete(position);
● The tRadioGroup component, is a collection of multiple different tRadioButton Components
● In the Radio Group component, to add more items search for the items property
● To change how the options are placed according to one another in a Radio Group
component, you must change the columns property
● The item index in a radio group component is responsible for letting the code know which
option was/is selected
● To select number or letters within a specific range use IN [‘1’..’4’] / [‘a’..’z’] which will perform a
function for number 1 to 4 or from letter a to z
● To use if AND statements, you need to have brackets around each statement
● The OR statement means that if at least one prompt is true, the code displays the desired
result
LOOPS
● There are 3 different loops:
❖ While loops
❖ Repeat Until loops
❖ For loops
● When using a While loop you manually have to initialize the variables before using the loop
● The while loop has a condition it has to meet, similar to an if statements condition
● A while loop can have multiple conditions in it
● A while loop will always return a true or false answer
● For Example:
i := 1;
while 1 <= 5 do
begin
redOutput.LInes.Add(IntToStr(i));
inc(i)
end;
This program will display the numbers from 1 to 5 on a rich edit component
This program will display the numbers from 1 to 5 on a rich edit component
● The ORD function will return the integer value(ordinal value) for a symbol/letter, for example
ORD(‘A’) will return 65
● The CHR function will return the character value for a symbol from an integer, for example
CHR(65) will return A.
● The IN function can be used to check whether something is equal to a group of values within
square brackets, for example sString[i] IN [‘A’,’B’,’C’] ~ inc(iCount) will increase the value of i
count so long as the character of sString is equal to A,B or C\
● WHEN USING IF STATEMENTS, <> MEANS DOES NOT EQUAL
● The sleep function causes for the program to pause/stop working for a set amount of
milliseconds
● An example of the sleep function is: sleep(10) which would stop the program for 10
milliseconds
● The Application.ProcessMessages is used to create sentient loops
● The Application.ProcessMessages is used to allow all the previous code within a loop to be
carried out before a sleep
● An example of Apllication.ProcessMessages:
● This will cause for the program to move the cars closer to each other every 10 milliseconds
Line 5(Change) iTotal:= iTotal + Line 5(Test) Until iTotal > 100;
iNumber;
end;
In the while loop, the loop doesn't necessarily run as the test comes before the change
whereas in the repeat until loop, the change comes before the test ensuring it always runs at
least once
● The TpageControl object allows you to create new pages which your program can switch
through
- You can right click on the the object to create new pages and can rename them in
the object manager
- The prefix for a tab sheet is ts
- Each tab sheet works as its own ‘form’ quote on quote
- You can add individual images to each page as a background by adding an
image to the page like you normally would for the form
- The tab visible property determines whether the user can see the tabs or not
- The resize event will carry out a certain code when the form/tab/object is
resized
- The on show event will carry out a certain code when an object/form/tab is
shown
- EXAMPLE OF PAGE:
● A message dialogue allows you to create a pop up message with buttons which users are
able to interact with
● To use a message dialogue, type
MessageDlg(‘Your text’,TMsgDlgType.mtTYPEOFDIALOGUE,[btnYes,btnNo],0
● When using POS, you can check if something already exists in a sentence by using the code:
Pos(sUsername,sString). If the answer you get is smaller than 1 the word does not exist but
if its larger than 1 the word does
● To use random range you must add the math directory