event driventheory

15
1 Unit 14 – Event Driven Programming What is event driven programming?

Upload: nickywalters

Post on 17-Dec-2014

9.936 views

Category:

Documents


3 download

DESCRIPTION

 

TRANSCRIPT

1

Unit 14 – Event Driven Programming

What is event driven programming?

22

Key Features of EDP

1. Forms & Controls

2. Trigger functions

3. Event handlers

4. Event loops

33

Forms and ControlsA 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)

44

Load Form

55

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.

66

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

77

Event HandlersAn 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

88

Event LoopsProgrammers 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

99

Event LoopUser interacts with page1

2 An ‘event’ is triggered

EVENT!

3 A code handler runs in responseThe page’s appearance is updated/modified as a result

4

1010

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

1111

FlexibilityWide 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

1212

Suitable for GUIsAre 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

1313

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 commandsErrors 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

1414

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

1515

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?