Designing an Event Driven Application
Welcome to your study notes for Designing an Event Driven Application! In this unit, you will learn how to design software that responds to actions—such as a user clicking a button, typing into a box, or moving a mouse. Whether you are aiming for top grades or just trying to wrap your head around programming design, these notes will guide you step-by-step.
Don't worry if this seems tricky at first! Traditional programming runs in a set order from line 1 to line 100. Event-Driven Programming (EDP) is different because the user decides what happens next. Designing these systems is a creative and logical process, and once you master the core patterns, it becomes second nature.
1. Understanding Event-Driven Programming (EDP)
In traditional procedural programming, the program controls the flow of execution in a rigid, step-by-step sequence. In Event-Driven Programming, the flow of execution is determined by events.
Real-World Analogy:
Think of a traditional program like a train on a track—it goes from Station A to Station B in a fixed line. An event-driven program is like a smart home—the lights stay off until a motion sensor is triggered, the door remains locked until someone enters a passcode, and the kettle only boils when you press the button.
The Three Core Elements: Properties, Methods, and Events (P-M-E)
Every interactive element on a screen (a control) is built around three core concepts:
• Properties: The characteristics or attributes of an object (for example: size, colour, text, visibility, or name).
Example: A button might have a \(Text\) property set to "Submit" and a \(BackColor\) set to "Blue".
• Methods: The built-in actions that an object can perform or have performed on it.
Example: A form has a method called \(Show()\) or \(Close()\).
• Events: The signals or triggers that occur when something happens, usually initiated by the user or the operating system.
Example: \(Click\), \(MouseEnter\), \(KeyPress\), or \(Tick\) (from a timer).
Memory Trick: Remember P-M-E:
- Properties = How it looks / what it has
- Methods = What it can do
- Events = What happens to it
How the Event Loop Works
1. The program starts and sets up the Graphical User Interface (GUI).
2. The program enters a continuous Event Loop (or message loop), waiting patiently for something to happen.
3. When the user interacts (e.g., clicks a button), an Event Trigger occurs.
4. The system calls an Event Handler (a specific block of code written to deal with that exact event).
5. Once the handler finishes executing, the application returns to the idle event loop waiting for the next event.
Key Takeaway: Event-driven programs are reactive. They wait in an event loop until an event is triggered, then execute the corresponding event handler code.
2. Principles of Graphical User Interface (GUI) Design
When designing an event-driven system, the User Interface (UI) is the bridge between the user and your code. If the UI is confusing, users will trigger the wrong events or make errors.
Key GUI Design Principles
• Consistency: Keep colours, fonts, button placements, and terminology the same across all screens in the application. If the "Save" button is green on the first form, it should not be red on the second.
• Simplicity and Clean Layout: Avoid clutter. Group related controls together using visual frames, panels, or logical white space.
• Clear Navigation: Make it obvious how to move between forms or cancel an action. Users should never feel "trapped" on a screen.
• Immediate Feedback: The interface must inform the user that their action was recognized. For example, show a progress bar during a file upload, display a confirmation message upon saving, or change button colour when clicked.
• Accessibility and Inclusivity: Design for all users by using high-contrast colours for text readability, sensible tab-order for keyboard navigation, and descriptive labels.
• Appropriate Choice of Controls: Choose the right control for the right task:
- TextBox: For free-form text input (e.g., entering a surname).
- Label: To display non-editable text or instructions.
- Button: To trigger a major action (e.g., Calculate, Save, Exit).
- RadioButton: When the user must select exactly one option from a small list (e.g., Gender: Male / Female / Other).
- CheckBox: When the user can select zero, one, or many options (e.g., Newsletter subscriptions).
- ComboBox / Dropdown: To select one option from a long list while saving screen space (e.g., Country selection).
- ListBox: To display a scrollable list of items where multiple selections might be needed.
Did You Know? Professional developers follow the "Rule of Least Surprise": software should always behave in a way that least surprises the user!
Key Takeaway: A great user interface is intuitive, consistent, provides instant feedback, and uses the right controls for data entry.
3. Design Tools and Techniques for Event-Driven Applications
Before writing a single line of code, software engineers use specific design artefacts to plan the layout, structure, and event flow.
1. Storyboards and Form Mockups
A storyboard or wireframe is a visual layout sketch of each form in the system. It shows:
• Where labels, textboxes, and buttons are placed.
• The exact text and titles displayed.
• The tab order (the order in which the cursor moves when pressing the Tab key).
2. Form Navigation Diagrams (Dialog Maps)
A form navigation diagram illustrates how forms connect to one another. It uses boxes to represent screens/windows and arrows to show navigation paths triggered by user actions (like clicking "Open Settings" or "Log Out").
3. Event-Action Tables (Event Tables)
An Event-Action Table is one of the most vital design tools in event-driven programming. It maps out the exact relationship between the control, the event, and the resulting action.
Example of an Event-Action Table for a Login Screen:
• Control Name: \(btnLogin\)
• Event: \(Click\)
• Action: Validate inputs; if valid, display \(frmDashboard\) and hide \(frmLogin\); otherwise show error message.
• Control Name: \(txtPassword\)
• Event: \(KeyPress\)
• Action: If the user presses the 'Enter' key, automatically trigger the \(btnLogin\) click event.
• Control Name: \(btnCancel\)
• Event: \(Click\)
• Action: Clear both textboxes and set focus back to \(txtUsername\).
4. State Transition Diagrams (STDs)
A State Transition Diagram shows the different states an application (or control) can be in, and what events cause a transition from one state to another.
Example: A Media Player application might have states like Stopped, Playing, and Paused. Clicking the "Play" button causes a transition from Stopped to Playing.
Key Takeaway: Planning tools—like Form Navigation Diagrams, Wireframes, and Event-Action Tables—ensure all user interactions are fully mapped out before coding begins.
4. Designing for Input Validation and Defensive UI
A major part of event-driven design is preventing runtime errors by making the interface defensive.
Strategies for Defensive Design:
• Enabling / Disabling Controls: Disable the "Submit" button (\(Enabled = False\)) until all mandatory fields have been completed by the user. This stops invalid submissions before they happen.
• Visual Cues: Change the background colour of a text box to light red if invalid data is entered, or display an error provider icon alongside helpful text.
• Input Masking & Selection Controls: Instead of asking users to type a date manually into a standard text box (which can lead to formatting errors like \(DD/MM/YYYY\) vs \(MM/DD/YYYY\)), provide a DatePicker or dropdown list to constrain inputs.
• Confirmation Dialogs: For irreversible events (such as deleting a customer record), always design a confirmation modal pop-up box asking "Are you sure you want to delete this record?" with Yes and No options.
Key Takeaway: Good event-driven design guides the user to enter correct data and uses confirmation prompts for destructive actions.
5. Standard Naming Conventions in Event-Driven Design
To keep code manageable, every control designed on a form should follow a clear, standard prefix naming convention (often called Hungarian notation or camelCase prefixes).
• Button: prefix \(btn\) (e.g., \(btnCalculate\), \(btnExit\))
• TextBox: prefix \(txt\) (e.g., \(txtFirstName\), \(txtTotalCost\))
• Label: prefix \(lbl\) (e.g., \(lblResult\), \(lblErrorMessage\))
• ComboBox / Dropdown: prefix \(cbo\) or \(cmb\) (e.g., \(cboCounty\))
• CheckBox: prefix \(chk\) (e.g., \(chkTermsAgreed\))
• RadioButton: prefix \(rad\) or \(rdo\) (e.g., \(radStandardDelivery\))
• Form: prefix \(frm\) (e.g., \(frmMain\), \(frmCustomerDetails\))
Common Mistake to Avoid: Leaving controls with their default names (like \(Button1\), \(TextBox3\)). This makes event handler code confusing and difficult to debug during development.
6. Chapter Summary & Quick Revision Checklist
Before moving on to the next topic, check if you can confidently explain:
• The difference between procedural and event-driven programming.
• The definition of Properties, Methods, and Events with examples.
• How an event loop and event handlers work together.
• Key GUI design principles (consistency, feedback, navigation, accessibility).
• How to construct and interpret an Event-Action Table.
• Techniques for defensive UI design (disabling controls, confirmation messages).
Quick Memory Aid: The C-L-E-A-R Interface
- Consistency (uniform layouts and fonts)
- Logical Flow (intuitive tab-order and grouping)
- Error Prevention (masks, dropdowns, disabled buttons)
- Accessibility (readable contrast and simple navigation)
- Responsive Feedback (status bars, prompt messages)