C++ Windows Forms L10 - Instantiate

931 views

Published on

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
931
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C++ Windows Forms L10 - Instantiate

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

×