Event-Based Programming: Roger Crawfis
Event-Based Programming: Roger Crawfis
Roger Crawfis
Window-based
programming
Most modern desktop systems are
window-based.
WhatlocationdoIusetosetthispixel?
Non-window based environment
Event-based Programming
Window-based GUIs are typically
comprised of many independent
processes.
These processes sit and wait for the
user to do something, to which it can
respond.
Moreover, a simple application may
be waiting for any of a number of
things.
Event-based Programming
Sample main programs:
C# / managed C++
static void Main()
{
Application.Run( new Form1());
}
GLUT
void main(int argc, char** argv)
{
myInit();
glutMainLoop();
}
Programming forms
statement
statement
statement
--------------statement
Procedural programming
Event-driven programming
Do
method 1
then
method 3
method 1
method 2
method 3
--------------method n
Do
method 2
then
method 1
then
method 3
1
2
3
n
Procedural programming
Up to now, our control flow model has been
pretty straight-forward.
Execution starts at main() and executes
statement sequentially branching with if,for,and
while statement, and occasional method calls.
When we need user input we call read() on the
console stream which waits (blocks) until the
user types something, then returns.
One problem with this model is: How do we wait
for and respond to input from more than one
source (eg keyboard and mouse). If we block
on either waiting for input, we may miss input
from the other.
Event-driven programming
the system waits for user input
events, and these events trigger
program methods
Event-based programming
addresses the two problems:
How to wait on multiple input sources
(input events)
How to decide what to do on each
type of input event
Event loop
main(){
...set up application data structures...
...set up GUI....
// enter event loop
while(true){
Event e = get_event();
Optional
Menu
Three
containers in
Frame with
Border Layout
UI-components
inside
containers each
with own layout
Frame with
normal window
controls
Each UI-component
{label}
{Frame}
components
{textfield}
{button}
is a class
with paint method
& lists of
Event listeners
Understanding .NET UI
Components
Overview
Replacement for VB Forms and MFC
Overview (cont)
Rich set of controls
ActiveX Interoperability
Can host ActiveX controls / author ActiveX controls
Printing Support
Visual inheritance
Create a form based on another form by using
inheritance
Advanced layout
Anchoring and docking
Basic Terms
Component
Timer, DB Connection
Web Forms
ASP.NET specific
Using VS.NET
Adding Components and Controls
Just drag component from the toolbox
Editing properties
Edit directly in the properties editor
this->MouseEnter += new
System.EventHandler(this.UserControl1_MouseEnter)
;
Events
Click mouse click on the control
SizeChanged - resize
InitializeComponent()
method
This method is created by VS.NET
Code generated there represents all the
changes done in the design view
Note: if you remove event handler or
data member added by VS.NET
manually, do not forget to clean the code
that uses it from InitializeComponent().
Doing this from the design view is easier!