C++ Windows Forms L02 - Controls P1

71
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L02 – Controls Part 1

description

C++ Windows Forms L02 - Controls P1 of C++ Windows Forms Light Course

Transcript of C++ Windows Forms L02 - Controls P1

Page 1: C++ Windows Forms L02 - Controls P1

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L02 – Controls Part 1

Page 2: C++ Windows Forms L02 - Controls P1
Page 3: C++ Windows Forms L02 - Controls P1

Controls

• Form• Button• CheckBox• CheckedListBox• ComboBox• Label• ListBox• PictureBox

• ProgressBar• RadioButton• TextBox• Panel• TabControl• DataGridView• Timer

Page 4: C++ Windows Forms L02 - Controls P1
Page 5: C++ Windows Forms L02 - Controls P1
Page 6: C++ Windows Forms L02 - Controls P1
Page 7: C++ Windows Forms L02 - Controls P1
Page 8: C++ Windows Forms L02 - Controls P1

Windows Forms

Page 9: C++ Windows Forms L02 - Controls P1

Form’s Properties

Form’s Events

The Form is selected

Page 10: C++ Windows Forms L02 - Controls P1
Page 11: C++ Windows Forms L02 - Controls P1

• Applications :– Design Time VS runtime

• Controls :– Properties

• Width , color .. etc

– Events• MouseClick , MouseHover , DragDrop .. etc

Control Properties and Events

Page 12: C++ Windows Forms L02 - Controls P1

Your First Form: Form1

Page 13: C++ Windows Forms L02 - Controls P1

– Properties :• BackColor• AutoSize• Font• Location• Opacity• Size (width , Height )• Text

Form’s Properties

Page 14: C++ Windows Forms L02 - Controls P1

– Event :• Load (imp.)• Click• KeyUp \ down \ ….• MouseOver \ Down \ leave \ … • ResizeBegin \ End• TextChange• Validation• FormClosing• Form Closed

Form’s Events

Page 15: C++ Windows Forms L02 - Controls P1

Form at Design Time

With Form1 being selected

Page 16: C++ Windows Forms L02 - Controls P1

Form at Design Time

With Form1 being selected

Page 17: C++ Windows Forms L02 - Controls P1

.ico extension

Page 18: C++ Windows Forms L02 - Controls P1
Page 19: C++ Windows Forms L02 - Controls P1
Page 20: C++ Windows Forms L02 - Controls P1

Forms Live TestingChanging Properties at runtime through Events

Page 21: C++ Windows Forms L02 - Controls P1

Button

Page 22: C++ Windows Forms L02 - Controls P1
Page 23: C++ Windows Forms L02 - Controls P1

23

Page 24: C++ Windows Forms L02 - Controls P1
Page 25: C++ Windows Forms L02 - Controls P1

– Properties :– Name , Text , Font , image , Location , Size , TabStop , TabIndex ,

Visible , BackColor

– Event :– MouseClick , MouseDown , MouseLeave , Resize , SizeChange ,

TextChanged , VisibleChanged , KeyUp , KeyDown , DragDrop

Button

Page 26: C++ Windows Forms L02 - Controls P1

Changing properties at runtime

• Considering we have the following design (at design time)

Page 27: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

button1 -> Text="I'm button1!!";

}

Control Property Valid value

Changing properties at runtime

• Adding the following button event

Page 28: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

button1 -> Text="I'm button1!!";

}

Object (intsance)Reference button by

its name

Data Member Valid value

Changing properties at runtime

• Adding the following button event

Page 29: C++ Windows Forms L02 - Controls P1

After clicking the button

Changing properties at runtime

Page 30: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

Form1 -> Text=" Hello World ”;

}

Member Valid value

Changing properties at runtime

Page 31: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

Form1 -> Text=" Hello World ”;

}

Member Valid value

Changing properties at runtime

Page 32: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

Form1 -> Text=" Hello World ”;

}

Member Valid value

Changing properties at runtime

• Now , let’s change the “Form”’s Text property .

Page 33: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

this -> Text="Hello World";

}

Member Value

Changing properties at runtime

Object at runtime (reference to Form1)Since we are inside

the Form1 class

Page 34: C++ Windows Forms L02 - Controls P1

After Clicking the Button

Changing properties at runtime

Page 35: C++ Windows Forms L02 - Controls P1

Changing properties at runtime

• Now , let’s change the “Form”’s Opacity property .

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

this -> Opacity=20; }

Class Member Valid value

Page 36: C++ Windows Forms L02 - Controls P1

Changing properties at runtime

• Now , let’s change the “Form”’s Opacity property .

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

this -> Opacity=20; }

Class Member Valid value

Page 37: C++ Windows Forms L02 - Controls P1

Changing properties at runtime

• Now , let’s change the “Form”’s Opacity property .

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

this -> Opacity=0.2; }

Class Member Valid value

Page 38: C++ Windows Forms L02 - Controls P1

After Clicking the Button

Changing properties at runtime

Page 39: C++ Windows Forms L02 - Controls P1

Message Box

Page 40: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

MessageBox::Show("Hello!!!");

}

MessageBox

Page 41: C++ Windows Forms L02 - Controls P1

Let the .NET write for you.Intellisense is ready to help you!

(Problem in 2010 and afterward)

Page 42: C++ Windows Forms L02 - Controls P1
Page 43: C++ Windows Forms L02 - Controls P1
Page 44: C++ Windows Forms L02 - Controls P1
Page 45: C++ Windows Forms L02 - Controls P1
Page 46: C++ Windows Forms L02 - Controls P1

21 Overload!

Page 47: C++ Windows Forms L02 - Controls P1

FormHide() vs Close()

Page 48: C++ Windows Forms L02 - Controls P1

What’s the difference between these two:

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

System::EventArgs^ e)

{

this->Hide();

}

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

System::EventArgs^ e)

{

this->Close();

}

Form Hide() vs Close()

Page 49: C++ Windows Forms L02 - Controls P1

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

sender, System::EventArgs^ e)

{

this->Hide();

}

Consider that we have only one form and we write the following code:

The form won’t be hidden on loading!

Form – Hide problem!

Page 50: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

this->Hide();

}

What happens now when breaking the project and run it again?

It won’t compile! When hiding the form , the project is still processed!!!So we should terminate its process first from task manager

Form – Hide problem!

Page 51: C++ Windows Forms L02 - Controls P1

Opacity Problem , too!

Page 52: C++ Windows Forms L02 - Controls P1

The applications are hidden butnot closed. All the memoryallocated by each hiddenapplication is still there

Page 53: C++ Windows Forms L02 - Controls P1

Application class

Page 54: C++ Windows Forms L02 - Controls P1

Application class

• Hierarchy :– System.Windows.Forms

• Methods :– Run – Exit

Page 55: C++ Windows Forms L02 - Controls P1
Page 56: C++ Windows Forms L02 - Controls P1

• Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Application::Exit();

Application class – Exit Method

Page 57: C++ Windows Forms L02 - Controls P1

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

System::EventArgs^ e)

{

Application::Exit();

}

Application class – Exit Method

• What will happen now? The whole application (with all forms) will simply close and the resources will be freed.

Page 58: C++ Windows Forms L02 - Controls P1

• What’s the difference?

Application::Exit();

this->Close();

Exit() vs Close()

Page 59: C++ Windows Forms L02 - Controls P1

Managed vs Un-ManagedCODE

Page 60: C++ Windows Forms L02 - Controls P1

• Usually …– Managed : .NET– Un-Managed : C++

• gcnew

– Garbage collector

Managed VS Un-Managed

Page 61: C++ Windows Forms L02 - Controls P1

• Usually …– Managed : .NET– Un-Managed : C++

• gcnew

– Garbage collector

Managed VS Un-Managed

Page 62: C++ Windows Forms L02 - Controls P1

• Stack \ heap “Un-Managed heap” \ managed heap

• What’s the problem then?!!

int MyVar=10; \\ stack

int *Ptr=new int; \\ Un-Managed heap

int ^Ptr=gcnew int; \\ managed heap

Managed VS Un-Managed

Page 63: C++ Windows Forms L02 - Controls P1

• Let’s have the following, anything wrong?

int *Ptr=new int; \\ Un-Managed heap

Ptr=NULL;

It’s a leaking in memory!

int ^Ptr=gcnew int; \\ managed heap

Ptr=NULL;

Not a leak anymore!

Managed VS Un-Managed

Page 64: C++ Windows Forms L02 - Controls P1

• Let’s have the following, anything wrong?

int *Ptr=new int; \\ Un-Managed heap

Ptr=NULL;

It’s a leaking in memory!

int ^Ptr=gcnew int; \\ managed heap

Ptr=NULL;

Not a leak anymore!

Managed VS Un-Managed

Page 65: C++ Windows Forms L02 - Controls P1

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

System::EventArgs^ e)

{

Drawing::Size *S;

S->Height=200;

S->Width=300;

this->Size=*S;

}

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

System::EventArgs^ e)

{

Drawing::Size *S=gcnew Drawing::Size;

S->Height=200;

S->Width=300;

this->Size=*S;

}

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

System::EventArgs^ e)

{

Drawing::Size *S=new Drawing::Size;

S->Height=200;

S->Width=300;

this->Size=*S;

}

there’s no new \ unmanaged

gcnew with * \ unmanaged with managed

WORKS! \ unmanaged

Which one of these works?

Page 66: C++ Windows Forms L02 - Controls P1

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

System::EventArgs^ e)

{

Drawing::Size *S=new Size;

S->Height=200;

S->Width=300;

this->Size=*S;

} Un-Known Size identifier “3’laza :D”

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

System::EventArgs^ e)

{

Drawing::Size ^S=gcnew Drawing::Size;

S->Height=200;

S->Width=300;

this->Size=*S;

} Works! \ managed

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

System::EventArgs^ e)

{

Drawing::Size ^S=gcnew Drawing::Size;

S->Height=200;

S->Width=300;

this->Size=^S;

} Compiler error .. No such ^

Which one of these works?

Page 67: C++ Windows Forms L02 - Controls P1

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

System::EventArgs^ e)

{

Drawing::Size ^S;

S->Height=200;

S->Width=300;

this->S=^S;

}

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

System::EventArgs^ e)

{

Drawing::Size ^S=new Drawing::Size;

S->Height=200;

S->Width=300;

this->Size=^S;

}

Error .. ^

Should be gcnew , not new

Which one of these works?

Page 68: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click_1(System::Object^

sender, System::EventArgs^ e)

{

this->Size=Drawing::Size(200,300);

}

Works!

Which one of these works?

Page 69: C++ Windows Forms L02 - Controls P1

private: System::Void button1_Click_1(System::Object^

sender, System::EventArgs^ e)

{

this->Size=Drawing::Size(200,300);

}

Works!

Which one of these works?

Page 70: C++ Windows Forms L02 - Controls P1

References

msdn Awesome libraryC++ , C# , VB.NET , ASP.NET , XNA , XML ... etchttp://msdn.microsoft.com/en-us/library/default.aspx

Page 71: C++ Windows Forms L02 - Controls P1

That’s it for today!