46
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L10-Instantiate Controls At Runtime

C++ Windows Forms L10 - Instantiate

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: C++ Windows Forms L10 - Instantiate

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L10-Instantiate Controls At Runtime

Page 2: C++ Windows Forms L10 - Instantiate
Page 3: C++ Windows Forms L10 - Instantiate
Page 4: C++ Windows Forms L10 - Instantiate
Page 5: C++ Windows Forms L10 - Instantiate

Instantiate Controls at Runtime

Page 6: C++ Windows Forms L10 - Instantiate

The Concept of Instantiating Controls at

Runtime

Page 7: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

• Let’s have the following form

Page 8: C++ Windows Forms L10 - Instantiate

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;

}

Page 9: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

NO!

Page 10: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

• What happens?

private: System::Void Form1_Load(System::Object^ sender,

System::EventArgs^ e)

{

Button ^MyButton = gcnew Button;

MyButton->Show();

}

Page 11: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

Page 12: C++ Windows Forms L10 - Instantiate

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;

}

Page 13: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

• Why?

Page 14: C++ Windows Forms L10 - Instantiate

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;

}

Page 15: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

Page 16: C++ Windows Forms L10 - Instantiate

How can we fire events on the newly created button?

Page 17: C++ Windows Forms L10 - Instantiate

The event wire-up in design time

• Consider that we have the following design …

Page 18: C++ Windows Forms L10 - Instantiate

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");

}

Page 19: C++ Windows Forms L10 - Instantiate

The event wire-up in design time

• Now, we can see the following …

Page 20: C++ Windows Forms L10 - Instantiate

The event wire-up in design time

Page 21: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

• Now, let’s add sth else!private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

MessageBox::Show("HiiiIiIIiIIIIIiiii");

}

Page 22: C++ Windows Forms L10 - Instantiate

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;

}

Page 23: C++ Windows Forms L10 - Instantiate

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);

}

Page 24: C++ Windows Forms L10 - Instantiate

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!!! ");

}

Page 25: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

Page 26: C++ Windows Forms L10 - Instantiate

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);

}

Page 27: C++ Windows Forms L10 - Instantiate

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?

Page 28: C++ Windows Forms L10 - Instantiate

Instantiating at Runtime

Page 29: C++ Windows Forms L10 - Instantiate

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 :'( ");

}

Page 30: C++ Windows Forms L10 - Instantiate

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);

}

Page 31: C++ Windows Forms L10 - Instantiate

Now, let’s see some more advanced stuff

Page 32: C++ Windows Forms L10 - Instantiate

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);

}

Page 33: C++ Windows Forms L10 - Instantiate

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);

}

}

Page 34: C++ Windows Forms L10 - Instantiate

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);

}

}

Page 35: C++ Windows Forms L10 - Instantiate

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);

}

}

Page 36: C++ Windows Forms L10 - Instantiate

Event handling

• Test it yourself. After seconds “without” motion the button becomes like this:

How can we solve this and see the motion?

Page 37: C++ Windows Forms L10 - Instantiate

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);

}

}

Page 38: C++ Windows Forms L10 - Instantiate

Event handling

Page 39: C++ Windows Forms L10 - Instantiate

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);

}

Page 40: C++ Windows Forms L10 - Instantiate

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);

}

}

Page 41: C++ Windows Forms L10 - Instantiate

Event handling

Mouse still here

Page 42: C++ Windows Forms L10 - Instantiate

Event handling

Now leaving the button area

Page 43: C++ Windows Forms L10 - Instantiate

Event handling

?

Page 44: C++ Windows Forms L10 - Instantiate

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)

Page 45: C++ Windows Forms L10 - Instantiate

Test it live!

Page 46: C++ Windows Forms L10 - Instantiate

That’s it for today!