delphi l02 controls p1
TRANSCRIPT
![Page 1: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshakergtr.wordpress.com
Intro to Event-driven Programming and Forms with Delphi@ZGTRShaker
2010, 2011, 2012
Intro to Event-driven Programming and Forms with Delphi
L02 – Controls P1
![Page 2: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/2.jpg)
![Page 3: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/3.jpg)
• Label • Edit• ComboBox• CheckBox• GroupBox• RadioButton• RadioGroup• ListBox
What’s for today?
![Page 4: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/4.jpg)
Tips
![Page 5: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/5.jpg)
General ‘Info’s & Tips
• Don’t forget the CTRL+Space auto-completion – CTRL+Space: code in-sight\auto-completion.
• You can’t write in Arabic in a component’s “NAME”.• Delphi is a not a “Case Sensitive ” language.• dpr *: when saving, the project you have will be saved as a
‘dpr’ termination.
*dpr: Delphi Project
![Page 6: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/6.jpg)
Runtime
• Form the upper bar > Run > Run.• Form the button in the upper bar.• Or F9
![Page 7: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/7.jpg)
Sth to Remember
• Differentiate between Visible & Enabled in Component’s properties.
• Differentiate between Name & Caption in Component’s properties.
• F9: RUN.• F9+CTRL: Compile.
![Page 8: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/8.jpg)
Changing your “.exe” icon
• “.ico” files extension• Project > Options > Application > Load Icon• It appears in the upper left corner of the application form
![Page 9: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/9.jpg)
Chase the button Exercise Do it & enjoy catching it :D
![Page 10: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/10.jpg)
Label
![Page 11: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/11.jpg)
Example
• Design this
![Page 12: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/12.jpg)
Label
• Is an Info• It can be used as an output as a “String”• “caption” manipulating.
Label1.caption:= ‘Hello World’;
String & only String
![Page 13: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/13.jpg)
Edit
![Page 14: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/14.jpg)
Example
• Design this
![Page 15: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/15.jpg)
Pre-Made String Functions
• StrAlloc – Allocates a character buffer of a given size on the heap.
• StrBufSize – Returns the size of a character buffer allocated using StrAlloc or
StrNew.
• StrCat – Concatenates two strings.
• StrComp – Compares two strings.
• StrCopy – Copies a string.
![Page 16: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/16.jpg)
Pre-Made String Functions
• StrDispose – Disposes a character buffer allocated using StrAlloc or StrNew.
• StrECopy – Copies a string and returns a pointer to the end of the string.
• StrEnd – Returns a pointer to the end of a string.
• StrFmt – Formats one or more values into a string.
• StrIComp – Compares two strings without case sensitivity.
• StrLCat – Concatenates two strings with a given maximum length of the resulting
string.
![Page 17: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/17.jpg)
Pre-Made String Functions
• StrLComp – Compares two strings for a given maximum length.
• StrLCopy – Copies a string up to a given maximum length.
• StrLen – Returns the length of a string.
• StrLFmt – Formats one or more values into a string with a given maximum length.
• StrLIComp – Compares two strings for a given maximum length without case sensitivity.
• StrLower – Converts a string to lowercase.
• StrMove – Moves a block of characters from one string to another.
![Page 18: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/18.jpg)
Pre-Made String Functions
• StrNew – Allocates a string on the heap.
• StrPCopy – Copies a Pascal string to a null-terminated string.
• StrPLCopy – Copies a Pascal string to a null-terminated string with a given maximum length.
• StrPos – Returns a pointer to the first occurrence of a given substring within a string.
• StrRScan – Returns a pointer to the last occurrence of a given character within a string.
• StrScan – Returns a pointer to the first occurrence of a given character within a string.
• StrUpper – Converts a string to uppercase.
![Page 19: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/19.jpg)
Pre-Made String Functions
• What we need the most:– String to integer : StrToInt()– Integer to string : IntToStr()
![Page 20: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/20.jpg)
Edit Prop.
• No “Caption” Prop., “Text”.• PassWord• Visible, Enabled• Hint, ShowHint• Top, Left
![Page 21: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/21.jpg)
Edit
• Text:“String” inner valued.• Can be used as an input & output.
• Have a supposition value (text Prop.).– Can be changed.
• ReadOnly Prop.
Var temp:integer;Edit1.Text:= temp; // output temp:= Edit1.Text; // input
![Page 22: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/22.jpg)
Edit
• Can be used to contain other variables types.• Input:
• Output:
Var temp:integer;temp:= StrToInt(Edit1.Text);
Var temp:String;Edit1.Text:= IntToStr(temp); //no need for -IntToStr-
![Page 23: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/23.jpg)
CheckBoxa “boolean”
![Page 24: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/24.jpg)
CheckBox Properties
• Caption• “true OR false”• Can be used as a “boolean” input• “Checked” Prop. • Can be referenced to other “boolean” variable.
![Page 25: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/25.jpg)
Crack the code
If (CheckBox1.Checked=true) thenBegin
// codeEnd
Elseif (CheckBox2.checked=true) then
Begin// code
Endelse
Begin// code
End
![Page 26: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/26.jpg)
x:=5;If (CheckBox1.Checked=true) then
Beginx:=10;
EndElse
if ((CheckBox1.Checked=true) and (CheckBox2.checked=true)) then
Beginx:=11;
End;
CheckBox1: CheckedCheckBox2: Checked
x:=5;If (CheckBox1.Checked=true) then
Beginx:=10;
EndElse
if ((CheckBox1.Checked=false) and (CheckBox2.checked=false)) then
Beginx:=11;
End;
CheckBox1: Not CheckedCheckBox2: Checked
![Page 27: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/27.jpg)
x:=5;If (CheckBox1.Checked=false) then
Beginx:=10;
EndElse
if ((CheckBox1.Checked=true) Or (CheckBox2.checked=true)) then Begin
x:=11;End;
x:=5;If (CheckBox1.Checked=true) then
Beginx:=10;
Endif ((CheckBox1.Checked=false) Or (CheckBox2.checked=false)) then
Beginx:=11;
End;
CheckBox1: Not CheckedCheckBox2: Checked
CheckBox1: CheckedCheckBox2: Not Checked
![Page 28: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/28.jpg)
x:=5;If (CheckBox1.Checked=true) then
Beginx:=10;
EndElse
if
((CheckBox1.Checked=true)or(CheckBox2.checked=not(CheckBox1.Checked)))then
Beginx:=11;
End;
x:=5;If (CheckBox1.Checked=true) then
Beginx:=10;
EndElse
if ((CheckBox1.Checked=false) or (CheckBox2.checked=false)) then
Beginx:=11;
End;
CheckBox1: Not CheckedCheckBox2: Not Checked
CheckBox1: Not CheckedCheckBox2: Checked
![Page 29: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/29.jpg)
Crack the codex:=5;
If (CheckBox1.Checked=true) then
Begin
x:=10;
End;
Else
if (CheckBox2.Checked=true) then
Begin
x:=11;
End;
CheckBox1: Not Checked
CheckBox2: Not Checked
![Page 30: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/30.jpg)
Var Bool1,Bool2: boolean;Begin
Bool1: = CheckBox1.Checked; Bool2:-=false;If (Bool1=true) thenBegin
// your codeEndElse
if (Bool2=not(true)) then Begin
//your codeEndelseBegin
//your codeEnd
End;
![Page 31: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/31.jpg)
Var Bool1,Bool2: boolean; x:integer;Begin
x:=5;Bool1: = CheckBox1.Checked; Bool2:-=false;If (Bool1=true) thenBegin
x:=3;EndElse
if (Bool2=not(true)) then Begin
x:=x+1;End
End;
CheckBox1: Not Checked
Var Bool1,Bool2: boolean; x:integer;Begin
x:=5;Bool1: = CheckBox1.Checked; Bool2:-=true;If (Bool1=Bool2) thenBegin
x:=3;Endif (Bool2=not(not(Bool1)) then Begin
x:=x+1;End
End;
CheckBox1: Checked
![Page 32: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/32.jpg)
ListBox
![Page 33: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/33.jpg)
ListBox
![Page 34: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/34.jpg)
ListBox Prop.
• Properties:– Sorted:
• If “true”, sorted in ascending order, default false– Items:
• Items.add > (String) • Items.string > [index] > (String)
– ItemIndex:• ItemIndex > Return selected index [] > (Integer)• No “ItemIndex” change at Design time
– Columns:• Multiple “column”s in ListBox if greater than Zero “0”.
– MultiSelect:• Enables “multiselect” to be “selected” if “true”, default “false”.
– “Auto” ScrollBar
![Page 35: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/35.jpg)
ListBox example
procedure TForm2.Button1Click(Sender: TObject);var x:integer; s:string;begin//ItemsListBox1.Items.Add('hehe');
//Strings:=ListBox1.Items.Strings[0];Label1.caption:=s;
//ItemIndexx:=ListBox1.ItemIndex;Label2.caption:=inttostr(x);end;
![Page 36: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/36.jpg)
ComboBox
![Page 37: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/37.jpg)
ComboBox
• Look like “ListBox”, but it’s a one “Choice at a time” one. – No “MultiSelect” Prop.
![Page 38: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/38.jpg)
ComboBox Prop.
• Properties:– Text
• Text > (string) like a “caption” for “ComboBox”
– Items• The available “option”s for user.• Items.add > (String) // func. Runtime• Items.string > [index] > (String) // func. Runtime
– ItemIndex• ItemIndex > Return selected index [] > (Integer)• “ItemIndex” changable at Design time , in contrary to “ListBox”
– Sorted:• If “true”, sorted in ascending order, default false
![Page 39: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/39.jpg)
ComboBox Event
• Most important: Onchange• Code: let’s “just” change the option “3” times.
![Page 40: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/40.jpg)
ComboBox – Code example
procedure TForm2.ComboBox1Change(Sender: TObject);
begin
memo1.lines.add('krkrkrkr');
end;
![Page 41: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/41.jpg)
procedure TForm2.Button1Click(Sender: TObject);
var x:integer; s:string;
begin
if ComboBox1.ItemIndex=0 then
memo1.Lines.Add('You chose English')
else
begin
if ComboBox1.ItemIndex=1 then
memo1.Lines.Add('You chose Arabic')
else
begin
if ComboBox1.ItemIndex=2 then
memo1.Lines.Add('You chose German')
else
if ComboBox1.ItemIndex=3 then
memo1.Lines.Add('You chose French');
end;
end;
end;
//Now let us add this new block of code on Button1Click
![Page 42: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/42.jpg)
ComboBox – Code example
• Now, what is the output on “Memo1” (Just Think it as if it’s a Multi-Lines “Edit”) that can show just 5 lines, without a scrollbar, when we choose 3 options (Arabic, German, French) in row, and clicking the button after each choice?
• Think Think Think.
![Page 43: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/43.jpg)
RadioButton
![Page 44: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/44.jpg)
RadioButton
• What it looks like?• It’s like when we choose the ages, languages,
– example– It’s represent “constant values” for one “variables”
• So it’s like CheckBox that we learned but with a key difference.
![Page 45: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/45.jpg)
Components
Radio Button
![Page 46: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/46.jpg)
RadioButton - Differences
• Let’s have 3 CheckBox & 3 Radio buttons.
CheckBox RadioButton
We can select all 3(one or more)
We can’t select all 3(just one of them)
Not connected with each other Connected with each other
![Page 47: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/47.jpg)
RadioButton - Example
• Remember that only one option can be chosen at a time
Block of codeBlock of codeBlock of codeBlock of code
![Page 48: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/48.jpg)
Many ways to “CRACK” the restriction
• GroupBox• RadioGroup• Panel
![Page 49: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/49.jpg)
RadioGroupsimplifies the task of grouping radio buttons
![Page 50: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/50.jpg)
RadioGroup Prop.
• Item:– Numbers of option in the “RadioGroup” depends on “item” Prop.– each string in “item” makes a radio button appears in the group box
with string as its “caption”.
• ItemIndex: – determines which “RadioButton” is currently selected.
• It’s which we’ll deal with in code in “Runtime”.
• Column:– Display the radio buttons in a single column or in multiple columns.
![Page 51: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/51.jpg)
RadioGroup Prop.No “Checked” Prop., It’s “ItemIndex”
Live example.
![Page 52: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/52.jpg)
RadioGroup Prop.procedure TForm1.NextClick(Sender: TObject);
begin
ShowMessage(RadioGroup1.Items.Strings[RadioGroup1.ItemIndex]
+ ' with Index '
+ IntToStr(RadioGroup1.ItemIndex));
end;
![Page 53: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/53.jpg)
GroupBox
![Page 54: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/54.jpg)
GroupBox
• What it looks like?• Its main function is to arrange all the related “control”s in
the “form”.• The most known related “control” is “RadioGroup” as we have
seen.• “Caption” Prop. Labels the GroupBox• Remember “cut & paste” method.
![Page 55: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/55.jpg)
Adding component to GroupBox
• Tool Pallete > Choose what you want to add– Place it in the “GroupBox”
• Now you can deal with your GroupBox as a complete block that contains related “control”s
![Page 56: Delphi L02 Controls P1](https://reader034.vdocuments.net/reader034/viewer/2022042607/5553b489b4c905d9448b4c8d/html5/thumbnails/56.jpg)
See you!