Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Event driventheory


Published on

  • Be the first to comment

Event driventheory

  1. 1. Unit 14 – Event Driven Programming What is event driven programming? 1
  2. 2. Key Features of EDP1. Forms & Controls2. Trigger functions3. Event handlers4. Event loops 2
  3. 3. Forms and ControlsA GUI (graphical user interface) is what the usersees when the program runs and usually consistsof one or several formsEach form contains various controls whichallow the user to interact with the program indifferent ways e.g. Textboxes, Menus, ButtonsEach type of control has its own events e.g. aForm events include: Load (open for first time, set default values) KeyDown (when the user presses a key) 3
  4. 4. Load Form 4
  5. 5. Trigger functionsEvent driven programs respond to events triggeredby the user via the mouse or keyboardCommon user events include clicking the mouse,pressing a key, changing textDifferent controls have different events (events fora text box not the same for a button)The programmer chooses which event(s) to respondto by selecting the appropriate trigger function. 5
  6. 6. Button EventsEvent driven programs respond to events triggeredby the userEvery object has arange of triggerfunctions – one foreach possible event A Button has a Click event, a GotFocus event, a MouseHover event etc 6
  7. 7. Event HandlersAn event triggers the appropriate event handlerEvent handlers contain the code that runs when anevent occursThese event handlers are self-contained sections ofcode – also called Procedures or Subroutines VB.NET uses Sub /End Sub to start and end the codeThis allows the programmer to work on one eventof one control at a time and makes testing easier 7
  8. 8. Event LoopsProgrammers not normally aware of theseThe Event loop keeps testing the user interface tosee if anything has happened (e.g. clicking on abutton or pressing a key)When detected, the event is passed to the triggerfunction which then finds the appropriate eventhandler to run the code written for this purposeOther (non user) activities can also cause an eventto trigger – typically used in networking and whenreading from or writing to files 8
  9. 9. Event Loop1 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. 10. AdvantagesFlexibility: Wide choice of controls and trigger functionsSuitability 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. 11. FlexibilityWide choice of controls – can be dragged anddropped onto formGood choice of trigger functions – programmercan decide what will happen whenProgrammer can assist user with intuitiveinterface, restrict input to a limited range, providevisual feedback and context-sensitive help 11
  12. 12. Suitable for GUIsAre particularly suitable for event drivenprogramming - uses WIMP system Windows, Icons, Menus, PointersThey have many objects which the programmercan use to provide the user with alternative waysto interact with the program (mouse or keyboard)Most controls are independent – so code can benaturally modular 12
  13. 13. Simplicity and Ease of DevelopmentEvent-driven languages such as C#, VB.NET and VisualC++ allow a programmer to build up their programgradually with simple building blocksProgrammers can add and code one object at a time, usingsimple constructs Pre-written code and drop-down lists showing choice of possible commands Errors are highlighted and step through debuggingCode modules can be re-used or imported from libraries,built-in wizards can be usedOnline and local help files are readily available 13
  14. 14. DisadvantagesErrors can be more difficult to spot than withsimpler, procedural programsPrograms with complex GUIs may be slower toload and run than simpler programs – particularlyif RAM is insufficientPrograms with too many forms can be veryconfusing and/or frustrating for the user 14
  15. 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 orbrightness selected in a graphics applicationAllow a user to access commands withoutthe mouse, but using keyboard navigationonly? 15