Your SlideShare is downloading. ×
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
C++ Windows Forms L10 - Instantiate
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C++ Windows Forms L10 - Instantiate

579

Published on

C++ Windows Forms L10 - Instantiate of C++ Windows Forms Light Course

C++ Windows Forms L10 - Instantiate of C++ Windows Forms Light Course

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
579
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. C++.NET Windows Forms Course L10-Instantiate Controls At Runtime Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  • 2. Instantiate Controls at Runtime
  • 3. The Concept of Instantiating Controls at Runtime
  • 4. Instantiating at Runtime • Let’s have the following form
  • 5. 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,
  • 6. Instantiating at Runtime NO!
  • 7. Instantiating at Runtime • What happens? private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { Button ^MyButton = gcnew Button; MyButton->Show(); } sender,
  • 8. Instantiating at Runtime
  • 9. 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,
  • 10. Instantiating at Runtime • Why?
  • 11. 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; }
  • 12. Instantiating at Runtime
  • 13. How can we fire events on the newly created button?
  • 14. The event wire-up in design time • Consider that we have the following design …
  • 15. 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,
  • 16. The event wire-up in design time • Now, we can see the following …
  • 17. The event wire-up in design time
  • 18. Instantiating at Runtime private: System::Void button1_Click(System::Object^ • Now, let’s add sth System::EventArgs^ e) else! { MessageBox::Show("HiiiIiIIiIIIIIiiii"); } sender,
  • 19. 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; }
  • 20. 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); }
  • 21. 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!!! "); }
  • 22. Instantiating at Runtime
  • 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::Mamy); }
  • 24. 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"); }
  • 25. Instantiating at Runtime
  • 26. 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 :'( "); }
  • 27. 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); }
  • 28. Now, let’s see some more advanced stuff
  • 29. 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); }
  • 30. 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,
  • 31. 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); } }
  • 32. 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?
  • 33. Event handling • Test it yourself. After seconds “without” motion the button becomes like this: How can we solve this and see the motion?
  • 34. 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); } }
  • 35. Event handling
  • 36. 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); }
  • 37. 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); } }
  • 38. Event handling Mouse still here
  • 39. Event handling Now leaving the button area
  • 40. Event handling ?
  • 41. 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)
  • 42. Test it live!
  • 43. That’s it for today!

×