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,
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,
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!!! ");
}
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 :'( ");
}
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?
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)