CS341 Visual Programming Lecture 05 PDF

Summary

This lecture covers object-oriented programming (OOP) concepts in visual programming using C#. It explores examples of custom controls, inheritance, and polymorphism within the context of visual programming.

Full Transcript

CS341: Visual Programming WEEK-05: OOP in Visual Programming Reda M. Hussien Assistant Professor of Information Systems FACULTY OF COMPUTERS AND INFORMATION Kafr El-Shiekh University Table of Contents 1 Object-Oriented Programming in Visual Programming 2 Using OOP Concepts in C# for Custo...

CS341: Visual Programming WEEK-05: OOP in Visual Programming Reda M. Hussien Assistant Professor of Information Systems FACULTY OF COMPUTERS AND INFORMATION Kafr El-Shiekh University Table of Contents 1 Object-Oriented Programming in Visual Programming 2 Using OOP Concepts in C# for Custom Controls Encapsulation Inheritance Polymorphism Abstraction Composition Reda M. Hussien CS341: Visual Programming 12 November 2024 1 / 23 Introduction to OOP in Visual Programming Object-Oriented Programming (OOP) in visual programming involves using graphical elements to represent classes, objects, and other OOP concepts. This can simplify programming by allowing direct manipulation of visual representations. Classes and Objects: Represented as blocks or icons. Encapsulation: Data fields and methods nested within blocks. Inheritance: Linking or duplicating class blocks to show inheritance relationships. Polymorphism: Overriding base class methods through editing blocks. Abstraction: Hiding complex implementations behind simplified visual interfaces. Reda M. Hussien CS341: Visual Programming 12 November 2024 2 / 23 Overview of Custom Controls in C# Custom controls in C# allow for reusable UI components with encapsulated logic. Using OOP concepts like encapsulation, inheritance, polymorphism, and abstraction, developers can create flexible and modular controls. Encapsulation Inheritance Polymorphism Abstraction Composition Reda M. Hussien CS341: Visual Programming 12 November 2024 3 / 23 Encapsulation Example: Custom Button with Hover Effect 1 using System ; 2 using System. Drawing ; 3 using System. Windows. Forms ; 4 5 public class CustomButton : Button 6 { 7 private Color hoverColor = Color. LightBlue ; 8 public Color HoverColor 9 { 10 get { return hoverColor ; } 11 set { hoverColor = value ; } 12 } Reda M. Hussien CS341: Visual Programming 12 November 2024 4 / 23 13 protected override void OnMouseEnter ( EventArgs e ) 14 { 15 base. OnMouseEnter ( e ) ; 16 this. BackColor = hoverColor ; 17 } 18 protected override void OnMouseLeave ( EventArgs e ) 19 { 20 base. OnMouseLeave ( e ) ; 21 this. BackColor = SystemColors. Control ; 22 } 23 } Reda M. Hussien CS341: Visual Programming 12 November 2024 5 / 23 Explanation: The ’hoverColor’ field is encapsulated within the ’CustomButton’ class and exposed through the ’HoverColor’ property, containing the hover color behavior within the control. Reda M. Hussien CS341: Visual Programming 12 November 2024 6 / 23 Reda M. Hussien CS341: Visual Programming 12 November 2024 7 / 23 Inheritance Example: Enhanced TextBox with Watermark 1 using System ; 2 using System. Drawing ; 3 using System. Windows. Forms ; 4 public class WatermarkTextBox : TextBox 5 { 6 private string watermarkText = " Enter text here " ; 7 private Color watermarkColor = Color. Gray ; 8 public string WatermarkText 9 { 10 get { return watermarkText ; } 11 set { watermarkText = value ; Invalidate () ; } 12 } Reda M. Hussien CS341: Visual Programming 12 November 2024 8 / 23 13 protected override void OnPaint ( PaintEventArgs e ) { 14 base. OnPaint ( e ) ; 15 if ( string. IsNullOrEmpty ( this. Text ) ) 16 { 17 using ( Brush brush = new SolidBrush ( watermarkColor ) ) 18 { 19 e. Graphics. DrawString ( watermarkText , this. Font , brush , new PointF (0 , 0) ) ; 20 } 21 } 22 } 23 } Reda M. Hussien CS341: Visual Programming 12 November 2024 9 / 23 Explanation: Inherits from ’TextBox’ class to add watermark functionality, encapsulated within the ’WatermarkText’ property. Reda M. Hussien CS341: Visual Programming 12 November 2024 10 / 23 Reda M. Hussien CS341: Visual Programming 12 November 2024 11 / 23 Polymorphism Example: Blinking Label 1 using System ; 2 using System. Drawing ; 3 using System. Windows. Forms ; 4 public class BlinkingLabel : Label 5 { 6 private Timer blinkTimer ; 7 private bool isBlinking ; 8 private Color originalColor ; 9 public BlinkingLabel () { 10 blinkTimer = new Timer { Interval = 500 }; 11 blinkTimer. Tick += (s , e ) = > ToggleBlink () ; 12 } Reda M. Hussien CS341: Visual Programming 12 November 2024 12 / 23 13 public bool IsBlinking { 14 get { return isBlinking ; } 15 set { 16 isBlinking = value ; 17 if ( isBlinking ) { 18 originalColor = this. ForeColor ; 19 blinkTimer. Start () ; 20 } else { 21 blinkTimer. Stop () ; 22 this. ForeColor = originalColor ; 23 } 24 } 25 } Reda M. Hussien CS341: Visual Programming 12 November 2024 13 / 23 26 private void ToggleBlink () 27 { 28 this. ForeColor = this. ForeColor == Color. Red ? originalColor : Color. Red ; 29 } 30 } Reda M. Hussien CS341: Visual Programming 12 November 2024 14 / 23 Explanation: ’BlinkingLabel’ overrides the ’ForeColor’ property dynamically to simulate blinking behavior using polymorphism. Reda M. Hussien CS341: Visual Programming 12 November 2024 15 / 23 Reda M. Hussien CS341: Visual Programming 12 November 2024 16 / 23 Abstraction Example: Base Entry Control 1 using System ; 2 using System. Drawing ; 3 using System. Windows. Forms ; 4 public abstract class BaseEntryControl : Control 5 { 6 protected TextBox inputTextBox ; 7 public string Value 8 { 9 get { return inputTextBox. Text ; } 10 set { inputTextBox. Text = value ; } 11 } 12 Reda M. Hussien CS341: Visual Programming 12 November 2024 17 / 23 13 public abstract bool ValidateInput () ; 14 15 public BaseEntryControl () 16 { 17 inputTextBox = new TextBox { Location = new Point (0 , 0) , Width = 200 }; 18 Controls. Add ( inputTextBox ) ; 19 } 20 } Reda M. Hussien CS341: Visual Programming 12 November 2024 18 / 23 1 public class Em ailEntry Control : BaseEntryControl 2 { 3 public override bool ValidateInput () 4 { 5 return Value. Contains ( " @ " ) ; 6 } 7 } 8 public class Ph oneEntry Control : BaseEntryControl 9 { 10 public override bool ValidateInput () { 11 return long. TryParse ( Value , out _ ) ; 12 } 13 } Reda M. Hussien CS341: Visual Programming 12 November 2024 19 / 23 Explanation: ’BaseEntryControl’ defines common behavior for data entry controls, with validation logic abstracted and implemented in derived classes. Reda M. Hussien CS341: Visual Programming 12 November 2024 20 / 23 Composition Example: Search Box 1 using System ; 2 using System. Drawing ; 3 using System. Windows. Forms ; 4 5 public class SearchBox : UserControl { 6 private TextBox searchTextBox ; 7 private Button searchButton ; 8 9 public string SearchText { 10 get { return searchTextBox. Text ; } 11 set { searchTextBox. Text = value ; } 12 } Reda M. Hussien CS341: Visual Programming 12 November 2024 21 / 23 13 14 public event EventHandler SearchClicked ; 15 16 public SearchBox () { 17 searchTextBox = new TextBox { Location = new Point (0 , 0) , Width = 150 }; 18 searchButton = new Button { Text = " Search " , Location = new Point (160 , 0) }; 19 20 searchButton. Click += (s , e ) = > SearchClicked ?. Invoke ( this , EventArgs. Empty ) ; 21 22 Controls. Add ( searchTextBox ) ; Reda M. Hussien CS341: Visual Programming 12 November 2024 22 / 23 23 Controls. Add ( searchButton ) ; 24 } 25 } Explanation: ’SearchBox’ control combines a ’TextBox’ and ’Button’, providing a search UI component through composition. Reda M. Hussien CS341: Visual Programming 12 November 2024 23 / 23

Use Quizgecko on...
Browser
Browser