Upload
mohammad-shaker
View
568
Download
2
Embed Size (px)
DESCRIPTION
C++ Windows Forms L10 - Instantiate of C++ Windows Forms Light Course
Citation preview
Mohammad Shakermohammadshakergtr.wordpress.com
C++.NET Windows Forms Course@ZGTRShaker
C++.NET Windows Forms Course
L10-Instantiate Controls At Runtime
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)
• Does it show a button?!!!
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
}
Instantiating at Runtime
NO!
Instantiating at Runtime
• What happens?
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Show();
}
Instantiating at Runtime
Instantiating at Runtime
• Parent! What happens now?
private: System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Button ^MyButton = gcnew Button;
MyButton->Parent = this;
}
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_click event to button1
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show("HiiiIiIIiIIIIIiiii");
}
The event wire-up in design time
• Now, we can see the following …
The event wire-up in design time
Instantiating at Runtime
• Now, let’s add sth else!private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show("HiiiIiIIiIIIIIiiii");
}
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
private: System::Void Mamy(System::Object^ sender,
System::EventArgs^ e)
{
MessageBox::Show("Mamy is a great cook! :D");
}
• And change it accordingly like this?
Instantiating at Runtime
Instantiating at Runtime
• Now, Consider we have the following two functions
private: System::Void Mamy(System::Object^ sender,
System::EventArgs^ e)
{
this->Text = "Mamy";
}
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 handlingprivate: 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^ sender,
System::EventArgs^ e)
{
while (sender->Width < 200)
{
sender->Width+=3;
sender->Height+=1;
Threading::Thread::Sleep(100);
}
}
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
• Now what happens? And what should happen?
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);
}
}
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
• Now, let’s add the following …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);
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!