Successfully reported this slideshow.
Your SlideShare is downloading. ×

C++ Windows Forms L02 - Controls P1

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 71 Ad

More Related Content

Slideshows for you (20)

Similar to C++ Windows Forms L02 - Controls P1 (20)

Advertisement

More from Mohammad Shaker (20)

Recently uploaded (20)

Advertisement

C++ Windows Forms L02 - Controls P1

  1. 1. C++.NET Windows Forms Course L02 – Controls Part 1 Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  2. 2. Controls • • • • • • • • Form Button CheckBox CheckedListBox ComboBox Label ListBox PictureBox • • • • • • • ProgressBar RadioButton TextBox Panel TabControl DataGridView Timer
  3. 3. Windows Forms
  4. 4. Form’s Properties Form’s Events The Form is selected
  5. 5. Control Properties and Events • Applications : – Design Time VS runtime • Controls : – Properties • Width , color .. etc – Events • MouseClick , MouseHover , DragDrop .. etc
  6. 6. Your First Form: Form1
  7. 7. Form’s Properties – Properties : • • • • • • • BackColor AutoSize Font Location Opacity Size (width , Height ) Text
  8. 8. Form’s Events – Event : • • • • • • • • • Load Click KeyUp down …. MouseOver Down leave … ResizeBegin End TextChange Validation FormClosing Form Closed (imp.)
  9. 9. Form at Design Time With Form1 being selected
  10. 10. Form at Design Time With Form1 being selected
  11. 11. .ico extension
  12. 12. Forms Live Testing Changing Properties at runtime through Events
  13. 13. Button
  14. 14. 23
  15. 15. Button – Properties : – Name , Text , Font , image , Location , Size , TabStop , TabIndex , Visible , BackColor – Event : – MouseClick , MouseDown , MouseLeave , Resize , SizeChange , TextChanged , VisibleChanged , KeyUp , KeyDown , DragDrop
  16. 16. Changing properties at runtime • Considering we have the following design (at design time)
  17. 17. 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!!"; } Control Property Valid value
  18. 18. 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
  19. 19. Changing properties at runtime After clicking the button
  20. 20. Changing properties at runtime private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 -> Text=" Hello World ”; } Member Valid value
  21. 21. Changing properties at runtime private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 -> Text=" Hello World ”; } Member Valid value
  22. 22. Changing properties at runtime • Now , let’s change the “Form”’s Text property . private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 -> Text=" Hello World ”; } Member Valid value
  23. 23. Changing properties at runtime private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { this -> Text="Hello World"; } Object at runtime (reference to Form1) Since we are inside the Form1 class Member Value
  24. 24. Changing properties at runtime After Clicking the Button
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. Changing properties at runtime After Clicking the Button
  29. 29. Message Box
  30. 30. MessageBox private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { MessageBox::Show("Hello!!!"); }
  31. 31. Let the .NET write for you. Intellisense is ready to help you! (Problem in 2010 and afterward)
  32. 32. 21 Overload!
  33. 33. Form Hide() vs Close()
  34. 34. Form Hide() vs Close() What’s the difference between these two: private: System::Void button1_Click(System::Object^ System::EventArgs^ e) { this->Hide(); } sender, private: System::Void button1_Click(System::Object^ System::EventArgs^ e) { this->Close(); } sender,
  35. 35. Form – Hide problem! Consider that we have only one form and we write the following code: private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { this->Hide(); } The form won’t be hidden on loading!
  36. 36. Form – Hide problem! What happens now when breaking the project and run it again? private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { this->Hide(); } It won’t compile! When hiding the form , the project is still processed!!! So we should terminate its process first from task manager
  37. 37. Opacity Problem , too!
  38. 38. The applications are hidden but not closed. All the memory allocated by each hidden application is still there
  39. 39. Application class
  40. 40. Application class • Hierarchy : – System.Windows.Forms • Methods : – Run – Exit
  41. 41. Application class – Exit Method • Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. Application::Exit();
  42. 42. Application class – Exit Method • What will happen now? The whole application (with all forms) will simply close and the resources will be freed. private: System::Void Form1_Load(System::Object^ System::EventArgs^ e) { Application::Exit(); } sender,
  43. 43. Exit() vs Close() • What’s the difference? Application::Exit(); this->Close();
  44. 44. Managed vs Un-Managed CODE
  45. 45. Managed VS Un-Managed • Usually … – Managed : .NET – Un-Managed : C++ • gcnew – Garbage collector
  46. 46. Managed VS Un-Managed • Usually … – Managed : .NET – Un-Managed : C++ • gcnew – Garbage collector
  47. 47. Managed VS Un-Managed • Stack heap “Un-Managed heap” managed heap int MyVar=10; stack int *Ptr=new int; Un-Managed heap int ^Ptr=gcnew int; managed heap • What’s the problem then?!!
  48. 48. Managed VS Un-Managed • Let’s have the following, anything wrong? int *Ptr=new int; Ptr=NULL; Un-Managed heap It’s a leaking in memory! int ^Ptr=gcnew int; managed heap Ptr=NULL; Not a leak anymore!
  49. 49. Managed VS Un-Managed • Let’s have the following, anything wrong? int *Ptr=new int; Ptr=NULL; Un-Managed heap It’s a leaking in memory! int ^Ptr=gcnew int; managed heap Ptr=NULL; Not a leak anymore!
  50. 50. 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->Size=*S; } there’s no new unmanaged 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; gcnew with * unmanaged with managed } 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; WORKS! unmanaged }
  51. 51. 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
  52. 52. Which one of these works? private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { Drawing::Size ^S; S->Height=200; S->Width=300; this->S=^S; } Error .. ^ sender, 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; } Should be gcnew , not new
  53. 53. Which one of these works? private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { this->Size=Drawing::Size(200,300); } Works!
  54. 54. Which one of these works? private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { this->Size=Drawing::Size(200,300); } Works!
  55. 55. References msdn Awesome library C++ , C# , VB.NET , ASP.NET , XNA , XML ... etc http://msdn.microsoft.com/en-us/library/default.aspx
  56. 56. That’s it for today!

×