C++ Windows Forms L02 - Controls P1

Post on 25-Dec-2014

299 views 4 download

description

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

Transcript of 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

Controls

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

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

Windows Forms

Form’s Properties

Form’s Events

The Form is selected

• Applications :– Design Time VS runtime

• Controls :– Properties

• Width , color .. etc

– Events• MouseClick , MouseHover , DragDrop .. etc

Control Properties and Events

Your First Form: Form1

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

Form’s Properties

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

Form’s Events

Form at Design Time

With Form1 being selected

Form at Design Time

With Form1 being selected

.ico extension

Forms Live TestingChanging Properties at runtime through Events

Button

23

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

Visible , BackColor

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

TextChanged , VisibleChanged , KeyUp , KeyDown , DragDrop

Button

Changing properties at runtime

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

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

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

After clicking the button

Changing properties at runtime

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

sender, System::EventArgs^ e)

{

Form1 -> Text=" Hello World ”;

}

Member Valid value

Changing properties at runtime

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

sender, System::EventArgs^ e)

{

Form1 -> Text=" Hello World ”;

}

Member Valid value

Changing properties at runtime

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 .

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

After Clicking the Button

Changing properties at runtime

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

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

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

After Clicking the Button

Changing properties at runtime

Message Box

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

sender, System::EventArgs^ e)

{

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

}

MessageBox

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

(Problem in 2010 and afterward)

21 Overload!

FormHide() vs Close()

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

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!

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!

Opacity Problem , too!

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

Application class

Application class

• Hierarchy :– System.Windows.Forms

• Methods :– Run – Exit

• 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

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.

• What’s the difference?

Application::Exit();

this->Close();

Exit() vs Close()

Managed vs Un-ManagedCODE

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

• gcnew

– Garbage collector

Managed VS Un-Managed

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

• gcnew

– Garbage collector

Managed VS Un-Managed

• 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

• 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

• 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

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?

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?

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?

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

sender, System::EventArgs^ e)

{

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

}

Works!

Which one of these works?

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

sender, System::EventArgs^ e)

{

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

}

Works!

Which one of these works?

References

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

That’s it for today!