1. Unit 14 – Event Driven
Programming
What is event driven
programming?
1
2. Key Features of EDP
1. Forms & Controls
2. Trigger functions
3. Event handlers
4. Event loops
2
3. Forms and Controls
A GUI (graphical user interface) is what the user
sees when the program runs and usually consists
of one or several forms
Each form contains various controls which
allow the user to interact with the program in
different ways e.g. Textboxes, Menus, Buttons
Each type of control has its own events e.g. a
Form events include:
Load (open for first time, set default values)
KeyDown (when the user presses a key)
3
5. Trigger functions
Event driven programs respond to events triggered
by the user via the mouse or keyboard
Common user events include clicking the mouse,
pressing a key, changing text
Different controls have different events (events for
a text box not the same for a button)
The programmer chooses which event(s) to respond
to by selecting the appropriate trigger function.
5
6. Button Events
Event driven programs respond to events triggered
by the user
Every object has a
range of trigger
functions – one for
each possible event
A Button has a Click
event, a GotFocus
event, a MouseHover
event etc
6
7. Event Handlers
An event triggers the appropriate event handler
Event handlers contain the code that runs when an
event occurs
These event handlers are self-contained sections of
code – also called Procedures or Subroutines
VB.NET uses Sub /End Sub to start and end the code
This allows the programmer to work on one event
of one control at a time and makes testing easier
7
8. Event Loops
Programmers not normally aware of these
The Event loop keeps testing the user interface to
see if anything has happened (e.g. clicking on a
button or pressing a key)
When detected, the event is passed to the trigger
function which then finds the appropriate event
handler to run the code written for this purpose
Other (non user) activities can also cause an event
to trigger – typically used in networking and when
reading from or writing to files
8
9. Event Loop
1 User interacts with page 2 An ‘event’ is triggered
EVENT!
4 The page’s appearance is 3 A code handler runs in response
updated/modified as a result
9
10. Advantages
Flexibility:
Wide choice of controls and trigger functions
Suitability for GUI:
uses WIMP system (Windows, Icons, Menus, Pointers)
Simplicity and Ease of development:
Add and code one control at a time, allows a
programmer to build up their program gradually with
simple building blocks
10
11. Flexibility
Wide choice of controls – can be dragged and
dropped onto form
Good choice of trigger functions – programmer
can decide what will happen when
Programmer can assist user with intuitive
interface, restrict input to a limited range, provide
visual feedback and context-sensitive help
11
12. Suitable for GUIs
Are particularly suitable for event driven
programming - uses WIMP system
Windows, Icons, Menus, Pointers
They have many objects which the programmer
can use to provide the user with alternative ways
to interact with the program (mouse or keyboard)
Most controls are independent – so code can be
naturally modular
12
13. Simplicity and Ease of Development
Event-driven languages such as C#, VB.NET and Visual
C++ allow a programmer to build up their program
gradually with simple building blocks
Programmers can add and code one object at a time, using
simple constructs
Pre-written code and drop-down lists showing choice of
possible commands
Errors are highlighted and step through debugging
Code modules can be re-used or imported from libraries,
built-in wizards can be used
Online and local help files are readily available
13
14. Disadvantages
Errors can be more difficult to spot than with
simpler, procedural programs
Programs with complex GUIs may be slower to
load and run than simpler programs – particularly
if RAM is insufficient
Programs with too many forms can be very
confusing and/or frustrating for the user
14
15. Which control would a programmer
use to….?
Allow a Yes/ No answer only?
Allow a wide range of keyboard input?
Limit user input to the numbers 1, 2, 3 & 4?
Allow a user to subtly change the colour or
brightness selected in a graphics application
Allow a user to access commands without
the mouse, but using keyboard navigation
only?
15