CS341 Visual Programming Lecture 02 PDF

Document Details

ArdentValley

Uploaded by ArdentValley

Kafr El Sheikh University

2024

CS341

Reda M. Hussien

Tags

event-driven programming visual programming programming concepts computer science

Summary

This document is a lecture on event-driven programming. The summary details visual programming concepts, event handlers, and delegates. It is a computer science lecture, based on concepts and coding examples.

Full Transcript

CS341: Visual Programming WEEK-02: Event-Driven Programming Reda M. Hussien Assistant Professor of Information Systems FACULTY OF COMPUTERS AND INFORMATION Kafr El-Shiekh University Table of Contents 1 Event-Driven Programming Concepts 2 Key components in event-driven programming 3 Ha...

CS341: Visual Programming WEEK-02: Event-Driven Programming Reda M. Hussien Assistant Professor of Information Systems FACULTY OF COMPUTERS AND INFORMATION Kafr El-Shiekh University Table of Contents 1 Event-Driven Programming Concepts 2 Key components in event-driven programming 3 Handling User Input and Responding to Events 4 Benefits of Event-Driven Programming in Handling User Input Reda M. Hussien CS341: Visual Programming 21 October 2024 1 / 28 Event-Driven Programming Concepts Event-driven programming Event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs. Instead of executing sequentially, an event-driven application responds to events asynchronously. This approach is common in graphical user interfaces (GUIs), real-time systems, and networking applications. Reda M. Hussien CS341: Visual Programming 21 October 2024 2 / 28 Key components in event-driven programming Key components in event-driven programming Key components in event-driven programming include 1 Event handlers, 2 Delegates, and 3 Events. Reda M. Hussien CS341: Visual Programming 21 October 2024 3 / 28 1. Event Handlers An event handler is a function or method in a program that gets called in response to a specific event. It defines the actions that should occur when the event is triggered. Event handlers are commonly used in GUIs to respond to user actions, such as clicking a button or typing in a text field. Reda M. Hussien CS341: Visual Programming 21 October 2024 4 / 28 Characteristics of Event Handlers Asynchronous Execution: Event handlers run asynchronously in response to events, meaning they execute when an event occurs, not in the normal program flow. Specificity: Each event handler is designed to handle a specific event or a group of related events. Loose Coupling: Event handlers allow loose coupling between the event source (such as a button click) and the event response (the logic that executes when the button is clicked). This improves modularity in the codebase. Reda M. Hussien CS341: Visual Programming 21 October 2024 5 / 28 In a C# Windows Forms application, an event handler could be set up to handle a button click: 1 private void Button_Click ( object sender , EventArgs e ) 2 { 3 MessageBox. Show ( " Button clicked ! " ) ; 4 } In this example, ’Button_Click’ is an event handler that executes whenever the specified button is clicked. Reda M. Hussien CS341: Visual Programming 21 October 2024 6 / 28 2. Delegates delegate is a type-safe function pointer in languages like C# and CSharp. It defines a method signature and can be used to pass methods as arguments to other methods. Delegates are critical in event-driven programming because they define the method signature that an event handler must follow to respond to an event. Reda M. Hussien CS341: Visual Programming 21 October 2024 7 / 28 Characteristics of Delegates Type-Safe: Delegates ensure that the method signature matches the expected event handler signature, preventing runtime errors. Encapsulation of Methods: Delegates allow methods to be passed around and invoked indirectly, which is key for decoupling the event source from the event handler. Multicasting: In languages like C#, delegates can be multicast, meaning multiple methods can be invoked by the same delegate. Reda M. Hussien CS341: Visual Programming 21 October 2024 8 / 28 In C#, a delegate could be defined and used to refer to a method that matches its signature: 1 public delegate void MyDelegate ( string message ) ; 2 public void PrintMessage ( string msg ) 3 { 4 Console. WriteLine ( msg ) ; 5 } 6 MyDelegate del = PrintMessage ; 7 del ( " Hello , world ! " ) ; // Invokes PrintMessage via delegate Reda M. Hussien CS341: Visual Programming 21 October 2024 9 / 28 3. Events An event in programming is an action or occurrence that can trigger responses in the form of event handlers. Events are central to event-driven systems because they notify the application when something significant has happened, allowing it to respond. Events are built on delegates in languages like C# and provide a more structured way to implement event-driven behavior. Reda M. Hussien CS341: Visual Programming 21 October 2024 10 / 28 Characteristics of Events Triggered by External Factors: Events are typically triggered by external factors, such as user actions (clicks, key presses), system conditions (timeouts), or hardware interactions. Based on Delegates: Events are often implemented using delegates, where the event publisher defines a delegate that can be subscribed to by event handlers. Subscription Model: Events follow a subscription model, where one or more event handlers can subscribe to an event, and when the event occurs, all subscribers are notified. Reda M. Hussien CS341: Visual Programming 21 October 2024 11 / 28 In C#, an event might be defined and used as follows: 1 public delegate void MyEventHandler ( string message ) ; 2 public event MyEventHandler MessageReceived ; 3 public void SendMessage ( string msg ) 4 { 5 if ( MessageReceived != null ) 6 MessageReceived ( msg ) ; 7 } In this example, ‘MessageReceived‘ is an event based on the ‘MyEventHandler‘ delegate. When the ‘SendMessage‘ method is called, it raises the event if there are any subscribers (event handlers) attached. Reda M. Hussien CS341: Visual Programming 21 October 2024 12 / 28 Handling User Input and Responding to Events Handling User Input and Responding to Events Handling user input and responding to events is one of the core functionalities of event-driven programming, especially in GUI applications, web applications, and real-time systems. The user interacts with the interface, and the application reacts to those interactions via event handlers. Reda M. Hussien CS341: Visual Programming 21 October 2024 13 / 28 1. Capturing User Input User input refers to actions such as key presses, mouse clicks, touch gestures, or data entered into forms. Event-driven systems typically have event listeners or sensors monitoring user input. For example, in a desktop GUI application, user inputs are captured as events that are handled by event listeners or handlers: Mouse Events: Clicking, double-clicking, hovering, dragging, etc. Keyboard Events: Key press, key release, etc. Touch Events: Swipe, pinch, tap (in mobile applications). Reda M. Hussien CS341: Visual Programming 21 October 2024 14 / 28 2. Responding to Events Once the user input is captured, the system needs to respond by executing the appropriate event handler. The response could be: Updating the UI (e.g., showing a message, changing the layout, highlighting a field). Processing the input (e.g., sending form data to the server). Triggering further actions (e.g., opening a file, starting a download). Reda M. Hussien CS341: Visual Programming 21 October 2024 15 / 28 In a Windows Forms application, handling a button click event to display a message might look like this: 1 // Create event handler 2 private void myButton_Click ( object sender , EventArgs e ) 3 { 4 MessageBox. Show ( " Hello , World ! " ) ; 5 } 6 // Subscribe the button ’s Click event to the handler 7 myButton. Click += new EventHandler ( myButton_Click ) ; Here, when the button (‘myButton‘) is clicked, the ‘myButton_Click‘ event handler will respond by displaying a message box. Reda M. Hussien CS341: Visual Programming 21 October 2024 16 / 28 Benefits of Event-Driven Programming in Handling User Input Benefits of Event-Driven Programming in Handling User Input 1 Improved Interactivity: The ability to immediately respond to user actions enhances the interactivity and responsiveness of an application. 2 Decoupling: Event-driven programming separates the logic of what triggers an action (event) from how the action is handled (event handler), leading to a cleaner, more modular design. 3 Scalability: The event model supports multiple handlers for the same event and makes it easy to extend functionality without altering the core logic. Reda M. Hussien CS341: Visual Programming 21 October 2024 17 / 28 Conclusion Event-driven programming is an essential paradigm for building interactive systems. By leveraging event handlers, delegates, and events, it allows applications to respond dynamically to user input, system changes, and external stimuli. Whether it’s a user clicking a button or a network message being received, event-driven programming ensures that applications can react in real-time, providing a flexible, modular, and responsive experience. Reda M. Hussien CS341: Visual Programming 21 October 2024 18 / 28 title 1 2 < html > 3 < head > 4 < title > My Web Page 5 < meta charset = " UTF -8 " > 6 7 < body > 8 < h1 > Welcome to My Web Page 9 This is a paragraph of text. 10 Visit Example 11 12 Reda M. Hussien CS341: Visual Programming 21 October 2024 19 / 28 What Are Prime Numbers? Definition A prime number is a number that has exactly two divisors. Example 2 is prime (two divisors: 1 and 2). 3 is prime (two divisors: 1 and 3). 4 is not prime (three divisors: 1, 2, and 4). Reda M. Hussien CS341: Visual Programming 21 October 2024 20 / 28 title 2 is prime (two divisors: 1 and 2). Reda M. Hussien CS341: Visual Programming 21 October 2024 21 / 28 title 2 is prime (two divisors: 1 and 2). 3 is prime (two divisors: 1 and 3). Reda M. Hussien CS341: Visual Programming 21 October 2024 21 / 28 title 2 is prime (two divisors: 1 and 2). 3 is prime (two divisors: 1 and 3). 4 is not prime (three divisors: 1, 2, and 4). Reda M. Hussien CS341: Visual Programming 21 October 2024 21 / 28 There Is No Largest Prime Number Theorem There is no largest prime number. Proof. 1 Suppose p were the largest prime number. 4 But q + 1 is greater than 1, thus divisible by some prime number not in the first p numbers. Reda M. Hussien CS341: Visual Programming 21 October 2024 22 / 28 There Is No Largest Prime Number Theorem There is no largest prime number. Proof. 1 Suppose p were the largest prime number. 2 Let q be the product of the first p numbers. 4 But q + 1 is greater than 1, thus divisible by some prime number not in the first p numbers. Reda M. Hussien CS341: Visual Programming 21 October 2024 22 / 28 There Is No Largest Prime Number Theorem There is no largest prime number. Proof. 1 Suppose p were the largest prime number. 2 Let q be the product of the first p numbers. 3 Then q + 1 is not divisible by any of them. 4 But q + 1 is greater than 1, thus divisible by some prime number not in the first p numbers. Reda M. Hussien CS341: Visual Programming 21 October 2024 22 / 28 There Is No Largest Prime Number Theorem There is no largest prime number. Proof. 1 Suppose p were the largest prime number. 2 Let q be the product of the first p numbers. 3 Then q + 1 is not divisible by any of them. 4 But q + 1 is greater than 1, thus divisible by some prime number not in the first p numbers. The proof used reductio ad absurdum. Reda M. Hussien CS341: Visual Programming 21 October 2024 22 / 28 What’s Still To Do? Answered Questions How many primes are there? Open Questions Is every even number the sum of two primes? Reda M. Hussien CS341: Visual Programming 21 October 2024 23 / 28 What’s Still To Do? Answered Questions How many primes are there? Open Questions Is every even number the sum of two primes? Reda M. Hussien CS341: Visual Programming 21 October 2024 24 / 28 What’s Still To Do? Answered Questions Open Questions How many primes are there? Is every even number the sum of two primes? Reda M. Hussien CS341: Visual Programming 21 October 2024 25 / 28 [Goldbach, 1742] Christian Goldbach. A problem we should try to solve before the ISPN ’43 deadline, Letter to Leonhard Euler, 1742. Reda M. Hussien CS341: Visual Programming 21 October 2024 26 / 28 In a C# Windows Forms application, an event handler could be set up to handle a button click: 1 public class HelloWorld { 2 public static void main ( String [] args ) { 3 System. out. println ( " Hello , World ! " ) ; 4 } 5 } In this example, ’Button_Click’ is an event handler that executes whenever the specified button is clicked. Reda M. Hussien CS341: Visual Programming 21 October 2024 26 / 28 In a C# Windows Forms application, an event handler could be set up to handle a button click: 1 function y = squareThisNumber ( x ) 2 y = x ^2; 3 end 4 5 result = squareThisNumber (5) ; 6 disp ( result ) ; In this example, ’Button_Click’ is an event handler that executes whenever the specified button is clicked. Reda M. Hussien CS341: Visual Programming 21 October 2024 27 / 28 In a C# Windows Forms application, an event handler could be set up to handle a button click: 1 SELECT name , age FROM users WHERE age > 18 ORDER BY name ASC ; In this example, ’Button_Click’ is an event handler that executes whenever the specified button is clicked. Reda M. Hussien CS341: Visual Programming 21 October 2024 28 / 28

Use Quizgecko on...
Browser
Browser