Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Lesson 1 - Introduction

1. Event-driven programming involves programs responding to asynchronous input events rather than following a predefined sequence of input. 2. An event is a notification that something specific has occurred, like a user action. An event handler is the code executed in response to an event. 3. Events use the publisher-subscriber model, where the publisher class defines and raises events, and subscriber classes provide event handlers to accept events.

Uploaded by

mohed ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lesson 1 - Introduction

1. Event-driven programming involves programs responding to asynchronous input events rather than following a predefined sequence of input. 2. An event is a notification that something specific has occurred, like a user action. An event handler is the code executed in response to an event. 3. Events use the publisher-subscriber model, where the publisher class defines and raises events, and subscriber classes provide event handlers to accept events.

Uploaded by

mohed ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Event-Driven

Programming
Event-Driven Programming

A conventional model of computation has the program prescribe the


exact order of input.

Programs terminate once the input is exhausted.

Event-driven programs do not control the sequence in which input


events occur.
Event Handling

Definition
An event is a notification that something specific has occurred, such
as a mouse click on a graphical button.

Definition
The event handler is a segment of code that is executed in response
to an event.
Events are user actions such as key
press, clicks, mouse movements,
etc., or some occurrence such as
system generated notifications.
Applications need to respond to
events when they occur. For
example, interrupts. Events are used
for inter-process communication.
Using Delegates with Events
The events are declared and raised in a class and
associated with the event handlers using delegates
within the same class or some other class. The class
containing the event is used to publish the event. This
is called the publisher class. Some other class that
accepts this event is called the subscriber class.
Events use the publisher-subscriber model.
A publisher is an object that contains the
definition of the event and the delegate. The
event-delegate association is also defined in
this object. A publisher class object invokes
the event and it is notified to other objects.
A subscriber is an object that accepts the
event and provides an event handler. The
delegate in the publisher class invokes the
method (event handler) of the subscriber
class.
Examples

GUI applications: Model-View-Controller design

Embedded applications:
cell phones
car engines
airplanes

Computation as
interaction
[Stein, 1998]:
Computation is a community of persistent entities coupled
together by their ongoing interactive behavior . . .
Beginning and end, when present, are special cases that can
often be ignored.
Imperative and Event-Driven Paradigms Contrasted
Event Sources

Input to an event-driven program comes from autonomous


event sources.

Events occur asynchronously.

Example:
human,
robot sensors,
engine sensors
Event Properties

1 An event-driven program has no perceived stopping point.

2 The traditional read-eval-print loop does not explicitly appear.

3 An application processes an input and exits.


Model-View-Controller (MVC)
Model: the object being implemented. Ex: game, calculator.

Controller: input mechanisms. Ex: buttons, menus, combo boxes.

View: output.
Ex: Tic-Tac-Toe Model

Whose turn is it?

State of the board.

Has someone won?

Are there no empty


squares?
Event Handling in C#

Event handling in C# (and the other .NET languages) is similar to that


in Java

.NET has two approaches, Windows Forms and Windows Presentation


Foundation-we cover only the former (which is the original approach)

An application subclasses the Form predefined class (defined in


System.Windows.Forms)

There is no need to create a frame or panel in which to place the


GUI components

Label objects are used to place text in the window

Radio buttons are objects of the RadioButton

class
Event Handling in C#

Components are positioned by assigning a new Point object to


the Location property of the component

private RadioButton plain = new RadioButton();


plain.Location = new Point(100, 300);
plain.Text = ‘‘Plain’’;
controls.Add(plain);

All C# event handlers have the same protocol, the return type is void and the
two parameters are of types object and EventArgs
Event Handling in C#

An event handler can have any name

A radio button is tested with the Boolean Checked property of the


button

private void rb_CheckedChanged (object o,


EventArgs
e) {
if (plain.Checked)
...
}

To register an event, a new EventHandler object must be created and


added to the predefined delegate for the event.
Event Handling in C#

When a radio button changes from unchecked to checked, the


CheckedChanged event is raised

The associated delegate is referenced by the name of the event

If the handler was named rb_CheckedChanged, we could register it


on the radio button named plain with:

plain.CheckedChanged +=
new EventHandler (rb_CheckedChanged);
Summary

An event is a notification that something has occurred that requires


handling by an event handler

Java event handling is defined on the Swing components

C# event handling is the .NET model, which is similar to the Java


model

You might also like