SlideShare a Scribd company logo
C++.NET
Windows Forms Course
L10-Instantiate Controls At
Runtime

Mohammad Shaker
mohammadshakergtr.wordpress.com
C++.NET Windows Forms Course
@ZGTRShaker
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
Instantiate Controls at
Runtime
The Concept of
Instantiating Controls at
Runtime
Instantiating at Runtime
• Let’s have the following form
Instantiating at Runtime
• Now, let’s have the following code, what does it mean?
It’s just allocating a memory space for a new object (button)
private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
}

• Does it show a button?!!!

sender,
Instantiating at Runtime
NO!
Instantiating at Runtime
• What happens?

private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Show();
}

sender,
Instantiating at Runtime
Instantiating at Runtime
• Parent! What happens now?

private: System::Void Form1_Load(System::Object^
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
}

sender,
Instantiating at Runtime
• Why?
Instantiating at Runtime
• What happens?
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
}
Instantiating at Runtime
How can we fire events on the
newly created button?
The event wire-up in design time
• Consider that we have the following design …
The event wire-up in design time
• And we add a button lick event to button1
_
c
private: System::Void button1_Click(System::Object^
System::EventArgs^ e)
{
MessageBox::Show("HiiiIiIIiIIIIIiiii");
}

sender,
The event wire-up in design time
• Now, we can see the following …
The event wire-up in design time
Instantiating at Runtime
private: System::Void button1_Click(System::Object^
• Now, let’s add sth
System::EventArgs^ e) else!
{
MessageBox::Show("HiiiIiIIiIIIIIiiii");
}

sender,
Instantiating at Runtime
• Now, back to our Button:
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
}
Instantiating at Runtime
• We can do this:
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
MyButton->Click += gcnew System::EventHandler(this,
&Form1::button1_Click_1);
}
Instantiating at Runtime
• What happens when clicking the created Button “MyButton”
or Button1?
private: System::Void button1_Click_1(System::Object^
sender, System::EventArgs^ e)
{
MessageBox::Show("Wow!!! ");
}
Instantiating at Runtime
Instantiating at Runtime
• We can do this?
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
MyButton->Click += gcnew System::EventHandler(this,
&Form1::Mamy);
}
Instantiating at Runtime
• And change it accordingly like this?

private: System::Void Mamy(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show("Mamy is a great cook! :D");
}
Instantiating at Runtime
Instantiating at Runtime
• Now, Consider we have the following two functions
private: System::Void Mamy(System::Object^
System::EventArgs^ e)
{
this->Text = "Mamy";
}

sender,

private: System::Void Chocolate(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show("There's no chocolate to eat :'( ");
}
Instantiating at Runtime
• We can do this? Try it out!
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
MyButton->Click += gcnew System::EventHandler(this,
&Form1::Mamy);
MyButton->Click += gcnew System::EventHandler(this,
&Form1::Chocolate);
}
Now, let’s see some more
advanced stuff
Event handling
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
MyButton->Click += gcnew System::EventHandler(this,
&Form1::button1_Click_1);
MyButton->MouseHover += gcnew System::EventHandler(this,
&Form1::MyProdHover);
}
Event handling
• Compiler error, why?
private: void MyProdHover (System::Object^
System::EventArgs^ e)
{
while (sender->Width < 200)
{
sender->Width+=3;
sender->Height+=1;
Threading::Thread::Sleep(100);
}
}

sender,
dynamic_cast
• We use dynamic_cast
private: void MyProdHover (System::Object^ sender,
System::EventArgs^ e)
{
while ((dynamic_cast<Button^>(sender))->Width < 200)
{
(dynamic_cast<Button^>(sender))->Refresh();
(dynamic_cast<Button^>(sender))->Width+=3;
(dynamic_cast<Button^>(sender))->Height+=1;
Threading::Thread::Sleep(100);
}
}
dynamic_cast
• We can do this for sure
private: void MyProdHover (System::Object^ sender,
System::EventArgs^ e)
{
Button ^TempButton = (dynamic_cast<Button^>(sender));
while (TempButton->Width < 200)
{
TempButton->Width+=3;
TempButton->Height+=1;
Threading::Thread::Sleep(100);
}
}

• Now what happens? And what should happen?
Event handling
• Test it yourself. After seconds “without” motion the button
becomes like this:

How can we solve this and see the motion?
Event handling
• Refresh method!
private: void MyProdHover (System::Object^ sender,
System::EventArgs^ e)
{
Button ^TempButton = (dynamic_cast<Button^>(sender));
while (TempButton->Width < 200)
{
TempButton->Refresh();
TempButton->Width+=3;
TempButton->Height+=1;
Threading::Thread::Sleep(100);
}
}
Event handling
Event handling
private: System::Void Form1_Load(System::Object^
• Now, let’s adde) following …
the
System::EventArgs^

sender,

{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
MyButton->Location = System::Drawing::Point(10, 20);
MyButton->Name = L"button1";
MyButton->Size = System::Drawing::Size(75, 23);
MyButton->TabIndex = 0;
MyButton->Text = L"MyDynamicButton";
MyButton->UseVisualStyleBackColor = true;
MyButton->Click += gcnew System::EventHandler(this,
&Form1::button1_Click_1);
MyButton->MouseHover += gcnew System::EventHandler(this,
&Form1::MyProdHover);
MyButton->MouseLeave += gcnew System::EventHandler(this,
&Form1::MyProdLeave);
}
Event handling
• What will happen now?
private: void MyProdLeave (System::Object^ sender,
System::EventArgs^ e)
{
while ((dynamic_cast<Button^>(sender))->Width > 50)
{
(dynamic_cast<Button^>(sender))->Refresh();
(dynamic_cast<Button^>(sender))->Width-=3;
(dynamic_cast<Button^>(sender))->Height+=1;
Threading::Thread::Sleep(100);
}
}
Event handling

Mouse still here
Event handling

Now leaving the button area
Event handling

?
What you can do now
• Now, you can create
– Any control you want
• textBox, pictureBox, panel, label, …. etc

– How you want it
– Controls its behavior
– With the number you want (Save references in lists, array, dictionary!!,
…etc)
Test it live!
That’s it for today!
Ad

More Related Content

What's hot (20)

C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1 C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
final project for C#
final project for C#final project for C#
final project for C#
Benjamin Fulker
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
Mahmoud Samir Fayed
 
Ete programs
Ete programsEte programs
Ete programs
Mayur Wankhede
 
syed
syedsyed
syed
Syed Mohd Naqi Zaidi
 
SOLID principles with Typescript examples
SOLID principles with Typescript examplesSOLID principles with Typescript examples
SOLID principles with Typescript examples
Andrew Nester
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)
Darwin Durand
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
DAYANA RETO
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql Server
Darwin Durand
 
Java programs
Java programsJava programs
Java programs
jojeph
 
Docimp
DocimpDocimp
Docimp
KaivanShah30
 
Do you Promise?
Do you Promise?Do you Promise?
Do you Promise?
jungkees
 
BingoConsoleApp
BingoConsoleAppBingoConsoleApp
BingoConsoleApp
Imtiazur Syed
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
Dmitry Buzdin
 
Java script
Java scriptJava script
Java script
Shagufta shaheen
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
123
123123
123
htmrk
 
The Ring programming language version 1.5.1 book - Part 6 of 180
The Ring programming language version 1.5.1 book - Part 6 of 180The Ring programming language version 1.5.1 book - Part 6 of 180
The Ring programming language version 1.5.1 book - Part 6 of 180
Mahmoud Samir Fayed
 
A Matter Of Form: Access Forms to make reporting a snap (or a click)
A Matter Of Form: Access Forms to make reporting a snap (or a click)A Matter Of Form: Access Forms to make reporting a snap (or a click)
A Matter Of Form: Access Forms to make reporting a snap (or a click)
Alan Manifold
 
C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1 C++ Windows Forms L08 - GDI P1
C++ Windows Forms L08 - GDI P1
Mohammad Shaker
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
Mahmoud Samir Fayed
 
SOLID principles with Typescript examples
SOLID principles with Typescript examplesSOLID principles with Typescript examples
SOLID principles with Typescript examples
Andrew Nester
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)
Darwin Durand
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
DAYANA RETO
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql Server
Darwin Durand
 
Java programs
Java programsJava programs
Java programs
jojeph
 
Do you Promise?
Do you Promise?Do you Promise?
Do you Promise?
jungkees
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
Dmitry Buzdin
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
The Ring programming language version 1.5.1 book - Part 6 of 180
The Ring programming language version 1.5.1 book - Part 6 of 180The Ring programming language version 1.5.1 book - Part 6 of 180
The Ring programming language version 1.5.1 book - Part 6 of 180
Mahmoud Samir Fayed
 
A Matter Of Form: Access Forms to make reporting a snap (or a click)
A Matter Of Form: Access Forms to make reporting a snap (or a click)A Matter Of Form: Access Forms to make reporting a snap (or a click)
A Matter Of Form: Access Forms to make reporting a snap (or a click)
Alan Manifold
 

Viewers also liked (12)

C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
Mohammad Shaker
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and Objects
Mohammad Shaker
 
XNA L01–Introduction
XNA L01–IntroductionXNA L01–Introduction
XNA L01–Introduction
Mohammad Shaker
 
Mobile Software Engineering Crash Course - C01 Intro
Mobile Software Engineering Crash Course - C01 IntroMobile Software Engineering Crash Course - C01 Intro
Mobile Software Engineering Crash Course - C01 Intro
Mohammad Shaker
 
Delphi L01 Intro
Delphi L01 IntroDelphi L01 Intro
Delphi L01 Intro
Mohammad Shaker
 
C# Starter L03-Utilities
C# Starter L03-UtilitiesC# Starter L03-Utilities
C# Starter L03-Utilities
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
OpenGL Starter L01
OpenGL Starter L01OpenGL Starter L01
OpenGL Starter L01
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 
C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - Intro
Mohammad Shaker
 
C# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-upC# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-up
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
C# Starter L02-Classes and Objects
C# Starter L02-Classes and ObjectsC# Starter L02-Classes and Objects
C# Starter L02-Classes and Objects
Mohammad Shaker
 
Mobile Software Engineering Crash Course - C01 Intro
Mobile Software Engineering Crash Course - C01 IntroMobile Software Engineering Crash Course - C01 Intro
Mobile Software Engineering Crash Course - C01 Intro
Mohammad Shaker
 
C# Starter L03-Utilities
C# Starter L03-UtilitiesC# Starter L03-Utilities
C# Starter L03-Utilities
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - Intro
Mohammad Shaker
 
C# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-upC# Starter L01-Intro and Warm-up
C# Starter L01-Intro and Warm-up
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Ad

Similar to C++ Windows Forms L10 - Instantiate (20)

Introj Query Pt2
Introj Query Pt2Introj Query Pt2
Introj Query Pt2
kshyju
 
Teclado word
Teclado wordTeclado word
Teclado word
Dianithaa Mendoza'
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...
bhargavi804095
 
intro_gui
intro_guiintro_gui
intro_gui
filipb2
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5
Bhushan Mulmule
 
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docxLab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
smile790243
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdf
arjuntiwari586
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
Ditec esoft C# project
Ditec esoft C# project Ditec esoft C# project
Ditec esoft C# project
K.K.T Madhusanka
 
Ditec esoft C# project
Ditec esoft C# projectDitec esoft C# project
Ditec esoft C# project
K.K.T Madhusanka
 
Metode
MetodeMetode
Metode
Hendra Sinaga
 
10 awt event model
10 awt event model10 awt event model
10 awt event model
Bayarkhuu
 
Construct 2 Platformer: Step by Step
Construct 2 Platformer: Step by StepConstruct 2 Platformer: Step by Step
Construct 2 Platformer: Step by Step
Shahed Chowdhuri
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4
Bhushan Mulmule
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
UI Design From Scratch - Part 5 - transcript.pdf
UI Design From Scratch - Part 5 - transcript.pdfUI Design From Scratch - Part 5 - transcript.pdf
UI Design From Scratch - Part 5 - transcript.pdf
ShaiAlmog1
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
The three layers of testing
The three layers of testingThe three layers of testing
The three layers of testing
Bart Waardenburg
 
37c
37c37c
37c
Sireesh K
 
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docxExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
SusanaFurman449
 
Introj Query Pt2
Introj Query Pt2Introj Query Pt2
Introj Query Pt2
kshyju
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...
bhargavi804095
 
intro_gui
intro_guiintro_gui
intro_gui
filipb2
 
Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5Windows Forms For Beginners Part 5
Windows Forms For Beginners Part 5
Bhushan Mulmule
 
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docxLab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
smile790243
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdf
arjuntiwari586
 
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAMPROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
PROGRAMMING USING C#.NET SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
10 awt event model
10 awt event model10 awt event model
10 awt event model
Bayarkhuu
 
Construct 2 Platformer: Step by Step
Construct 2 Platformer: Step by StepConstruct 2 Platformer: Step by Step
Construct 2 Platformer: Step by Step
Shahed Chowdhuri
 
Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4Windows Forms For Beginners Part - 4
Windows Forms For Beginners Part - 4
Bhushan Mulmule
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
UI Design From Scratch - Part 5 - transcript.pdf
UI Design From Scratch - Part 5 - transcript.pdfUI Design From Scratch - Part 5 - transcript.pdf
UI Design From Scratch - Part 5 - transcript.pdf
ShaiAlmog1
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
The three layers of testing
The three layers of testingThe three layers of testing
The three layers of testing
Bart Waardenburg
 
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docxExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
ExplanationDesignerCodeGlobal.Microsoft.VisualBasic.Compiler.docx
SusanaFurman449
 
Ad

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
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
 
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 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
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
Mohammad Shaker
 
Indie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSevenIndie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSeven
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
 
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 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 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
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
Mohammad Shaker
 
Indie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSevenIndie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSeven
Mohammad Shaker
 

Recently uploaded (20)

Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
DNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in NepalDNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in Nepal
ICT Frame Magazine Pvt. Ltd.
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptxIn-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
aptyai
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
How Top Companies Benefit from Outsourcing
How Top Companies Benefit from OutsourcingHow Top Companies Benefit from Outsourcing
How Top Companies Benefit from Outsourcing
Nascenture
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptxIn-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
In-App Guidance_ Save Enterprises Millions in Training & IT Costs.pptx
aptyai
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdfComputer Systems Quiz Presentation in Purple Bold Style (4).pdf
Computer Systems Quiz Presentation in Purple Bold Style (4).pdf
fizarcse
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 

C++ Windows Forms L10 - Instantiate

  • 1. C++.NET Windows Forms Course L10-Instantiate Controls At Runtime Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  • 6. The Concept of Instantiating Controls at Runtime
  • 7. Instantiating at Runtime • Let’s have the following form
  • 8. Instantiating at Runtime • Now, let’s have the following code, what does it mean? It’s just allocating a memory space for a new object (button) private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { Button ^MyButton = gcnew Button; } • Does it show a button?!!! sender,
  • 10. Instantiating at Runtime • What happens? private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Show(); } sender,
  • 12. Instantiating at Runtime • Parent! What happens now? private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; } sender,
  • 14. Instantiating at Runtime • What happens? private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; }
  • 16. How can we fire events on the newly created button?
  • 17. The event wire-up in design time • Consider that we have the following design …
  • 18. The event wire-up in design time • And we add a button lick event to button1 _ c private: System::Void button1_Click(System::Object^ System::EventArgs^ e) { MessageBox::Show("HiiiIiIIiIIIIIiiii"); } sender,
  • 19. The event wire-up in design time • Now, we can see the following …
  • 20. The event wire-up in design time
  • 21. Instantiating at Runtime private: System::Void button1_Click(System::Object^ • Now, let’s add sth System::EventArgs^ e) else! { MessageBox::Show("HiiiIiIIiIIIIIiiii"); } sender,
  • 22. Instantiating at Runtime • Now, back to our Button: private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; }
  • 23. Instantiating at Runtime • We can do this: private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; MyButton->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1); }
  • 24. Instantiating at Runtime • What happens when clicking the created Button “MyButton” or Button1? private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("Wow!!! "); }
  • 26. Instantiating at Runtime • We can do this? private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; MyButton->Click += gcnew System::EventHandler(this, &Form1::Mamy); }
  • 27. Instantiating at Runtime • And change it accordingly like this? private: System::Void Mamy(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("Mamy is a great cook! :D"); }
  • 29. Instantiating at Runtime • Now, Consider we have the following two functions private: System::Void Mamy(System::Object^ System::EventArgs^ e) { this->Text = "Mamy"; } sender, private: System::Void Chocolate(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("There's no chocolate to eat :'( "); }
  • 30. Instantiating at Runtime • We can do this? Try it out! private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; MyButton->Click += gcnew System::EventHandler(this, &Form1::Mamy); MyButton->Click += gcnew System::EventHandler(this, &Form1::Chocolate); }
  • 31. Now, let’s see some more advanced stuff
  • 32. Event handling private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; MyButton->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1); MyButton->MouseHover += gcnew System::EventHandler(this, &Form1::MyProdHover); }
  • 33. Event handling • Compiler error, why? private: void MyProdHover (System::Object^ System::EventArgs^ e) { while (sender->Width < 200) { sender->Width+=3; sender->Height+=1; Threading::Thread::Sleep(100); } } sender,
  • 34. dynamic_cast • We use dynamic_cast private: void MyProdHover (System::Object^ sender, System::EventArgs^ e) { while ((dynamic_cast<Button^>(sender))->Width < 200) { (dynamic_cast<Button^>(sender))->Refresh(); (dynamic_cast<Button^>(sender))->Width+=3; (dynamic_cast<Button^>(sender))->Height+=1; Threading::Thread::Sleep(100); } }
  • 35. dynamic_cast • We can do this for sure private: void MyProdHover (System::Object^ sender, System::EventArgs^ e) { Button ^TempButton = (dynamic_cast<Button^>(sender)); while (TempButton->Width < 200) { TempButton->Width+=3; TempButton->Height+=1; Threading::Thread::Sleep(100); } } • Now what happens? And what should happen?
  • 36. Event handling • Test it yourself. After seconds “without” motion the button becomes like this: How can we solve this and see the motion?
  • 37. Event handling • Refresh method! private: void MyProdHover (System::Object^ sender, System::EventArgs^ e) { Button ^TempButton = (dynamic_cast<Button^>(sender)); while (TempButton->Width < 200) { TempButton->Refresh(); TempButton->Width+=3; TempButton->Height+=1; Threading::Thread::Sleep(100); } }
  • 39. Event handling private: System::Void Form1_Load(System::Object^ • Now, let’s adde) following … the System::EventArgs^ sender, { Button ^MyButton = gcnew Button; MyButton->Parent = this; MyButton->Location = System::Drawing::Point(10, 20); MyButton->Name = L"button1"; MyButton->Size = System::Drawing::Size(75, 23); MyButton->TabIndex = 0; MyButton->Text = L"MyDynamicButton"; MyButton->UseVisualStyleBackColor = true; MyButton->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1); MyButton->MouseHover += gcnew System::EventHandler(this, &Form1::MyProdHover); MyButton->MouseLeave += gcnew System::EventHandler(this, &Form1::MyProdLeave); }
  • 40. Event handling • What will happen now? private: void MyProdLeave (System::Object^ sender, System::EventArgs^ e) { while ((dynamic_cast<Button^>(sender))->Width > 50) { (dynamic_cast<Button^>(sender))->Refresh(); (dynamic_cast<Button^>(sender))->Width-=3; (dynamic_cast<Button^>(sender))->Height+=1; Threading::Thread::Sleep(100); } }
  • 42. Event handling Now leaving the button area
  • 44. What you can do now • Now, you can create – Any control you want • textBox, pictureBox, panel, label, …. etc – How you want it – Controls its behavior – With the number you want (Save references in lists, array, dictionary!!, …etc)
  • 46. That’s it for today!
  翻译: