SlideShare a Scribd company logo
Intro to Event-driven Programming
and Forms with Delphi
L08 - Instantiate Controls at Runtime
Part 2 and other things you need to
know

Mohammad Shaker
mohammadshakergtr.wordpress.com
Intro to Event-driven Programming and Forms with Delphi
@ZGTRShaker
2010, 2011, 2012
Instantiate Controls at
Runtime
Instantiate Controls at Runtime
• Let’s look at the “OnClickButton”

procedure TForm2.Button1Click(Sender: TObject);
begin
end;
Instantiate Controls at Runtime
Instantiate Controls at Runtime
• Can we Create Our Own “OnClick” Event?
– YES Sure we can
– But watch out for defining the exact function header as original one

procedure TForm2.ZGTRClick(Sender: TObject);
Begin
End;
Write it yourself
Instantiate Controls at Runtime
• Let’s have the following Form
Instantiate Controls at Runtime
• And let’s have the following hand-made Event :D

procedure TForm2.ZGTRClick(Sender: TObject);
var i: integer;
Begin
ShowMessage('In My Own Event ');
End;
Instantiate Controls at Runtime
Instantiate Controls at Runtime
procedure TForm2.Button1Click(Sender: TObject);
Var myButton:TButton;
begin
myButton:= TButton.Create(Self);
myButton.Parent:= Panel1;
myButton.Height:= 35;
myButton.Width:= 100;
myButton.Caption:= 'MyCreatedButton';
myButton.OnClick:= ZGTRClick;
// WOW:D
end;
Instantiate Controls at Runtime

Before Clicking the
Dynamic Button
Instantiate Controls at Runtime
• Cool, right?

After Clicking the
Dynamic Button
Instantiate Controls at Runtime
• Another Ex: Let’s Have The following Form
Instantiate Controls at Runtime
Var
i: integer;

// Global Variable

procedure TForm2.FormCreate(Sender: TObject);
begin
i:= 0;
// Initialize Variable
end;

procedure TForm2.ZGTRClick(Sender: TObject);
begin
ShowMessage('Am in my own created EVENT (procedure) '
+ Sender.ClassName);
end;
Instantiate Controls at Runtime
procedure TForm2.ZGTRClick(Sender: TObject);
begin
ShowMessage('Am in my own created EVENT (procedure) '
+ Sender.ClassName);
end;
Instantiate Controls at Runtime
procedure TForm2.Button1Click(Sender: TObject);
Var myButton:TButton;
myLabel:TLabel;
myEdit:TEdit;
begin
myButton:= TButton.Create(Self);
myButton.Parent:= Panel1;
myButton.Height:= 35;
myButton.Width:= 100;
myButton.Caption:= 'MyCreatedButton';
myButton.Top:= i*20;
myButton.OnClick:= ZGTRClick;
i:=i+2;
Instantiate Controls at Runtime
myLabel:= TLabel.Create(Self);
myLabel.Parent:= Panel1;
myLabel.Caption:= 'MyCreatedLabel';
myLabel.Top:= i*20;
myLabel.OnClick:= ZGTRClick;
i:=i+2;
myEdit:= TEdit.Create(Self);
myEdit.Parent:= Panel1;
myEdit.Text:= 'MyCreatedEdit';
myEdit.Top:= i*20;
myEdit.OnClick:= ZGTRClick;
end;
Instantiate Controls at Runtime

Cool For Now
Instantiate Controls at Runtime

Clicking The
Button
Instantiate Controls at Runtime

Clicking The
Label
Instantiate Controls at Runtime

Clicking The
Edit
Instantiate Controls at Runtime
• Another Ex: Let’s have the previous example with the
following form and modified “ZGTRClick” is like this.
Instantiate Controls at Runtime
procedure TForm2.ZGTRClick(Sender: TObject);
var i: integer;
Begin
i:=i+1;
End;
procedure TForm2.Button2Click(Sender: TObject);
begin
ShowMessage('i = ' + IntToStr(i));
end;
Instantiate Controls at Runtime
• What will happen now when clicking 4 times on any ones of
the 3 controls, and then clicking (Show”i”).
Nice Trick :D
• Another Ex:
– Let’s see the following Example
Nice Trick :D
var i: integer;

// Global Variable

procedure TForm2.FormCreate(Sender: TObject);
begin
i:= 0;
// Initialize Variable
end;
procedure TForm2.Button1Click(Sender: TObject);
Var myButton:TButton;
begin
i:= i+1;
myButton:= TButton.Create(Self);
myButton.Parent:= Panel1;
myButton.Top:= i*10;
myButton.Left:= 20;
myButton.OnClick:= Button1Click;
end;
Nice Trick :D
Clicking “Create”
button two times

After That
Clicking any ones
of created
buttons
Array of Objects
Array of Objects
• Declaration

X: Array of String;

M: Array of Array of Integer;
Array of Objects
Array of Objects
• Let’s have the following from design
Array of Objects
procedure TForm2.Button1Click(Sender: TObject);
var ButtonArr: Array of TButton;
begin
try
SetLength(ButtonArr,StrToInt(Edit1.Text));
for I:= 0 to StrToInt(Edit1.Text)-1 do
Begin
ButtonArr[i]:= TButton.Create(Self);
ButtonArr[i].Parent:= Panel1;
ButtonArr[i].Top:= i*30;
ButtonArr[i].Left:= 10;
End;
except
on e: Exception do
ShowMessage('Re-Enter an Integer value.');
end;
end;
Array of Objects
• Controls created at Runtime!
Exception Handling
• Let’s have the following Example
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
new(Ptr);
Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
Edit1.Text:= IntToStr(Ptr^);
end;
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
// Here’s The Error
Edit1.Text:= IntToStr(Ptr^);
end;

Runtime

Error
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
begin
Edit1.Text:= IntToStr(Ptr^);
end;

No Runtime

Error
Exception Handling
the concept
Exception Handling
• The structure of exception handling
try
// Code which may raise an exception.
except
on e:Exception do
begin
// Handle exception “e”
end;
finally
// Code which will be executed whether or not an exception is caught
end;
Exception Handling
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
i: integer;
begin
try
// Note that there’s no need for Begin-End In try Statement

Edit1.Text:= IntToStr(Ptr^);
Ptr^:= 3;
Edit1.Text:= IntToStr(Ptr^);
except
on e: Exception do
ShowMessage('Go away , my program is right
, ente el ajdab:D');
end;
end;

No bugs in my code:@
Exception Handling
Exception Handling
• No Msg
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
i: integer;
begin
try
Edit1.Text:= IntToStr(Ptr^);
except
on e: Exception do
ShowMessage('Go away , my program is right No bugs in my code:@
, ente el ajdab:D');
end;
end;
Exception Handling
• No Msg
procedure TForm2.Button1Click(Sender: TObject);
var Ptr: ^Integer;
i: integer;
begin
try
i:= Ptr^;
except
on e: Exception do
ShowMessage('Go away , my program is right No bugs in my code:@
, ente el ajdab:D');
end;
end;
Exception Handling
• Cool!
procedure TForm2.Button1Click(Sender: TObject);
var i: integer;
begin
try
i:= StrToInt(Edit1.Text);
except
on e: Exception do
ShowMessage(‘ Re-Enter Integer value. ‘);
end;
end;
Delphi Console Application
Can be used for testing, checking algorithms, tracking
variables before deploying your project into forms.
Delphi Console Application
Delphi Console Application
Delphi Console Application
Delphi Console Application
program Project2;
{$APPTYPE CONSOLE}

uses
SysUtils;

begin
try
{ TODO -oUser -cConsole Main: Insert code here }
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
Delphi Console Application
program Project2;

{$APPTYPE CONSOLE}
uses
SysUtils;
Var
x:integer;
begin
try
{ TODO -oUser -cConsole Main: Insert code here }
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln(x);
Writeln('x = ', x);
Readln;
end.
Delphi Console Application
And.. end of course
mohammadshakergtr@gmail.com
http://mohammadshakergtr.wordpress.com/
tweet @ZGTRShaker
See you!

More Related Content

Similar to Delphi L08 Controls at Runtime P2

Gui builder
Gui builderGui builder
Gui builderlearnt
 
Calculator Processing
Calculator ProcessingCalculator Processing
Calculator Processing
Hock Leng PUAH
 
5 Cool Things About PLSQL
5 Cool Things About PLSQL5 Cool Things About PLSQL
5 Cool Things About PLSQL
Connor McDonald
 
CSharp for Unity Day2
CSharp for Unity Day2CSharp for Unity Day2
CSharp for Unity Day2
Duong Thanh
 
SPF WinForm Programs
SPF WinForm ProgramsSPF WinForm Programs
SPF WinForm Programs
Hock Leng PUAH
 
The timer use
The timer useThe timer use
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
Huu Bang Le Phan
 
Spf chapter 03 WinForm
Spf chapter 03 WinFormSpf chapter 03 WinForm
Spf chapter 03 WinFormHock Leng PUAH
 
Hack2the future Microsoft .NET Gadgeteer
Hack2the future Microsoft .NET GadgeteerHack2the future Microsoft .NET Gadgeteer
Hack2the future Microsoft .NET Gadgeteer
Lee Stott
 
19.Advanced Visual Basic Lab.pdf
19.Advanced Visual Basic Lab.pdf19.Advanced Visual Basic Lab.pdf
19.Advanced Visual Basic Lab.pdf
virox10x
 
CrossUI Tutorial - Basic - Calling JS functions
CrossUI Tutorial  - Basic - Calling JS functionsCrossUI Tutorial  - Basic - Calling JS functions
CrossUI Tutorial - Basic - Calling JS functions
Jack Lee
 
XMetaL Dialog Odds & Ends
XMetaL Dialog Odds & EndsXMetaL Dialog Odds & Ends
XMetaL Dialog Odds & EndsXMetaL
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Java calculator
Java calculatorJava calculator
Java calculator
Sarah McNellis
 
Super components en Pascal
Super components en PascalSuper components en Pascal
Super components en Pascal
Paco Miró
 
Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3
Gordon Morrison
 

Similar to Delphi L08 Controls at Runtime P2 (20)

Gui builder
Gui builderGui builder
Gui builder
 
Calculator Processing
Calculator ProcessingCalculator Processing
Calculator Processing
 
5 Cool Things About PLSQL
5 Cool Things About PLSQL5 Cool Things About PLSQL
5 Cool Things About PLSQL
 
C# Loops
C# LoopsC# Loops
C# Loops
 
CSharp for Unity Day2
CSharp for Unity Day2CSharp for Unity Day2
CSharp for Unity Day2
 
SPF WinForm Programs
SPF WinForm ProgramsSPF WinForm Programs
SPF WinForm Programs
 
The timer use
The timer useThe timer use
The timer use
 
Reactive fsharp
Reactive fsharpReactive fsharp
Reactive fsharp
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
 
Spf chapter 03 WinForm
Spf chapter 03 WinFormSpf chapter 03 WinForm
Spf chapter 03 WinForm
 
Hack2the future Microsoft .NET Gadgeteer
Hack2the future Microsoft .NET GadgeteerHack2the future Microsoft .NET Gadgeteer
Hack2the future Microsoft .NET Gadgeteer
 
19.Advanced Visual Basic Lab.pdf
19.Advanced Visual Basic Lab.pdf19.Advanced Visual Basic Lab.pdf
19.Advanced Visual Basic Lab.pdf
 
CrossUI Tutorial - Basic - Calling JS functions
CrossUI Tutorial  - Basic - Calling JS functionsCrossUI Tutorial  - Basic - Calling JS functions
CrossUI Tutorial - Basic - Calling JS functions
 
XMetaL Dialog Odds & Ends
XMetaL Dialog Odds & EndsXMetaL Dialog Odds & Ends
XMetaL Dialog Odds & Ends
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Java calculator
Java calculatorJava calculator
Java calculator
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
Super components en Pascal
Super components en PascalSuper components en Pascal
Super components en Pascal
 
Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3Gordon morrison temporalengineering-delphi-v3
Gordon morrison temporalengineering-delphi-v3
 

More from Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Recently uploaded

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 

Recently uploaded (20)

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 

Delphi L08 Controls at Runtime P2

  • 1. Intro to Event-driven Programming and Forms with Delphi L08 - Instantiate Controls at Runtime Part 2 and other things you need to know Mohammad Shaker mohammadshakergtr.wordpress.com Intro to Event-driven Programming and Forms with Delphi @ZGTRShaker 2010, 2011, 2012
  • 2.
  • 4. Instantiate Controls at Runtime • Let’s look at the “OnClickButton” procedure TForm2.Button1Click(Sender: TObject); begin end;
  • 6. Instantiate Controls at Runtime • Can we Create Our Own “OnClick” Event? – YES Sure we can – But watch out for defining the exact function header as original one procedure TForm2.ZGTRClick(Sender: TObject); Begin End; Write it yourself
  • 7. Instantiate Controls at Runtime • Let’s have the following Form
  • 8. Instantiate Controls at Runtime • And let’s have the following hand-made Event :D procedure TForm2.ZGTRClick(Sender: TObject); var i: integer; Begin ShowMessage('In My Own Event '); End;
  • 10. Instantiate Controls at Runtime procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; begin myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Height:= 35; myButton.Width:= 100; myButton.Caption:= 'MyCreatedButton'; myButton.OnClick:= ZGTRClick; // WOW:D end;
  • 11. Instantiate Controls at Runtime Before Clicking the Dynamic Button
  • 12. Instantiate Controls at Runtime • Cool, right? After Clicking the Dynamic Button
  • 13. Instantiate Controls at Runtime • Another Ex: Let’s Have The following Form
  • 14. Instantiate Controls at Runtime Var i: integer; // Global Variable procedure TForm2.FormCreate(Sender: TObject); begin i:= 0; // Initialize Variable end; procedure TForm2.ZGTRClick(Sender: TObject); begin ShowMessage('Am in my own created EVENT (procedure) ' + Sender.ClassName); end;
  • 15. Instantiate Controls at Runtime procedure TForm2.ZGTRClick(Sender: TObject); begin ShowMessage('Am in my own created EVENT (procedure) ' + Sender.ClassName); end;
  • 16. Instantiate Controls at Runtime procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; myLabel:TLabel; myEdit:TEdit; begin myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Height:= 35; myButton.Width:= 100; myButton.Caption:= 'MyCreatedButton'; myButton.Top:= i*20; myButton.OnClick:= ZGTRClick; i:=i+2;
  • 17. Instantiate Controls at Runtime myLabel:= TLabel.Create(Self); myLabel.Parent:= Panel1; myLabel.Caption:= 'MyCreatedLabel'; myLabel.Top:= i*20; myLabel.OnClick:= ZGTRClick; i:=i+2; myEdit:= TEdit.Create(Self); myEdit.Parent:= Panel1; myEdit.Text:= 'MyCreatedEdit'; myEdit.Top:= i*20; myEdit.OnClick:= ZGTRClick; end;
  • 18. Instantiate Controls at Runtime Cool For Now
  • 19. Instantiate Controls at Runtime Clicking The Button
  • 20. Instantiate Controls at Runtime Clicking The Label
  • 21. Instantiate Controls at Runtime Clicking The Edit
  • 22. Instantiate Controls at Runtime • Another Ex: Let’s have the previous example with the following form and modified “ZGTRClick” is like this.
  • 23. Instantiate Controls at Runtime procedure TForm2.ZGTRClick(Sender: TObject); var i: integer; Begin i:=i+1; End; procedure TForm2.Button2Click(Sender: TObject); begin ShowMessage('i = ' + IntToStr(i)); end;
  • 24. Instantiate Controls at Runtime • What will happen now when clicking 4 times on any ones of the 3 controls, and then clicking (Show”i”).
  • 25. Nice Trick :D • Another Ex: – Let’s see the following Example
  • 26. Nice Trick :D var i: integer; // Global Variable procedure TForm2.FormCreate(Sender: TObject); begin i:= 0; // Initialize Variable end; procedure TForm2.Button1Click(Sender: TObject); Var myButton:TButton; begin i:= i+1; myButton:= TButton.Create(Self); myButton.Parent:= Panel1; myButton.Top:= i*10; myButton.Left:= 20; myButton.OnClick:= Button1Click; end;
  • 27. Nice Trick :D Clicking “Create” button two times After That Clicking any ones of created buttons
  • 29. Array of Objects • Declaration X: Array of String; M: Array of Array of Integer;
  • 31. Array of Objects • Let’s have the following from design
  • 32. Array of Objects procedure TForm2.Button1Click(Sender: TObject); var ButtonArr: Array of TButton; begin try SetLength(ButtonArr,StrToInt(Edit1.Text)); for I:= 0 to StrToInt(Edit1.Text)-1 do Begin ButtonArr[i]:= TButton.Create(Self); ButtonArr[i].Parent:= Panel1; ButtonArr[i].Top:= i*30; ButtonArr[i].Left:= 10; End; except on e: Exception do ShowMessage('Re-Enter an Integer value.'); end; end;
  • 33. Array of Objects • Controls created at Runtime!
  • 34.
  • 35. Exception Handling • Let’s have the following Example
  • 36. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin new(Ptr); Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; Edit1.Text:= IntToStr(Ptr^); end;
  • 37. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; // Here’s The Error Edit1.Text:= IntToStr(Ptr^); end; Runtime Error
  • 38. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; begin Edit1.Text:= IntToStr(Ptr^); end; No Runtime Error
  • 40. Exception Handling • The structure of exception handling try // Code which may raise an exception. except on e:Exception do begin // Handle exception “e” end; finally // Code which will be executed whether or not an exception is caught end;
  • 41. Exception Handling procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try // Note that there’s no need for Begin-End In try Statement Edit1.Text:= IntToStr(Ptr^); Ptr^:= 3; Edit1.Text:= IntToStr(Ptr^); except on e: Exception do ShowMessage('Go away , my program is right , ente el ajdab:D'); end; end; No bugs in my code:@
  • 43. Exception Handling • No Msg procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try Edit1.Text:= IntToStr(Ptr^); except on e: Exception do ShowMessage('Go away , my program is right No bugs in my code:@ , ente el ajdab:D'); end; end;
  • 44. Exception Handling • No Msg procedure TForm2.Button1Click(Sender: TObject); var Ptr: ^Integer; i: integer; begin try i:= Ptr^; except on e: Exception do ShowMessage('Go away , my program is right No bugs in my code:@ , ente el ajdab:D'); end; end;
  • 45. Exception Handling • Cool! procedure TForm2.Button1Click(Sender: TObject); var i: integer; begin try i:= StrToInt(Edit1.Text); except on e: Exception do ShowMessage(‘ Re-Enter Integer value. ‘); end; end;
  • 46. Delphi Console Application Can be used for testing, checking algorithms, tracking variables before deploying your project into forms.
  • 50. Delphi Console Application program Project2; {$APPTYPE CONSOLE} uses SysUtils; begin try { TODO -oUser -cConsole Main: Insert code here } except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.
  • 51. Delphi Console Application program Project2; {$APPTYPE CONSOLE} uses SysUtils; Var x:integer; begin try { TODO -oUser -cConsole Main: Insert code here } except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; Readln(x); Writeln('x = ', x); Readln; end.
  • 53.
  • 54. And.. end of course
  • 56.