SlideShare a Scribd company logo
Event-based Programming
Roger Crawfis
Window-based
programming
• Most modern desktop systems are
window-based.
Non-window based environment
Window based environment
What location do I use to set this pixel?
Event-based Programming
• Window-based GUI’s 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());
}
– C++ (MFC Document/View architecture)
There isn’t one!!
– GLUT
void main(int argc, char** argv)
{
myInit();
glutMainLoop();
}
Programming forms
 Procedural programming
 code is executed in sequential order.
 Event-driven programming
 code is executed upon activation of events.
statement 1
statement 2
statement 3
--------
--------
statement n
method 1
method 2
method 3
--------
--------
method n
Do
method 1
then
method 3
Do
method 2
then
method 1
then
method 3
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
General form of event-
driven programming
• The operating system and window
system process input events from
devices (mouse movement, mouse
clicks, key presses etc)
• The window system decides which
window, and hence which frame and
program, each input event belongs to.
General form of event-driven
programming
• A data structure describing the event is
produced and placed on an event queue.
Events are added to one end of the
queue by the window system. Events are
removed from the queue and processed
by the program. These event data
structures hold information including:
– Which of the program's windows this event
belongs to.
– The type of event (mouse click, key press,
etc.)
– Any event-specific data (mouse position, key
code, etc.)
Event loop
• main(){
• ...set up application data structures...
• ...set up GUI....
• // enter event loop
• while(true){
• Event e = get_event();
• process_event(e); // event dispatch routine } }
• the event loop is usually hidden from
programmer, he/she provides just a
process_event() method
AWT Applications - Frame
• Frame is a container for components
Frame with
normal window
controls
Optional Menu
Three
containers in
Frame with
Border Layout
UI-components
inside
containers each
with own layout
Understanding the GUI
• UI-containers
– have list of
UI-components
• Each UI-component
– is a class
– with paint method
– & lists of
Event listeners
{Frame}
{label}
{textfield}
{button}
components
Understanding .NET UI
Components
• Overview
– Replacement for VB Forms and MFC
• Fully integrated with the .NET framework
• Web Service aware
• Data (ADO.NET and XML) aware
• Secure code and Licensing
– A framework for building Windows
application
• “No touch” deployment
– No registration, Versionable, Smart caching
– A RAD environment in Visual Studio .NET
• Windows Form Designer, Property Browser
Overview (cont)
– Rich set of controls
• Standard controls - Label, Button, TextBox, etc.
• Common controls - ProgressBar, StatusBar, etc.
• Data aware controls - ListBox, DataGrid, etc.
• Common dialogs
– 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
• Control (User Control)
– Windows Forms
• Menu
• Button
• etc
– 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
• Registering for events
– Also in the properties editor
What actually happens?
• Data members for components
• For example, after placing timer:
public class FunnyButton :
System.Windows.Forms.Button
{
private System.Windows.Forms.Timer
timer1;
What actually happens?
• Attributes for the properties
• For example, after changing Text
property:
private void InitializeComponent()
{
…
this.Text = "ADO Example";
What actually happens?
• Delegates for events
– Special type event in .NET (special delegate)
private void InitializeComponent()
{
…
this->MouseEnter += new
System.EventHandler(this.UserControl1_MouseEnter
);
…
}
…
private void UserControl1_MouseEnter(object sender,
System.EventArgs e) {}
Names everyone should
know
• Properties
– Name – data member name
– Text – text shown by control (was
Caption)
– ForeColor – current text color
– Dock – docking to the parent
– Enabled – active or not
• 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!

More Related Content

Similar to cse581_03_EventProgramming.ppt (20)

PPT
VC++ Fundamentals
ranigiyer
 
PPTX
Event handling in Java(part 1)
RAJITHARAMACHANDRAN1
 
PPTX
UNIT 1 Overview of GUI programing-1.pptx
NarendraKamble8
 
PPTX
LECTURE 12 WINDOWS FORMS PART 2.pptx
AOmaAli
 
PPTX
Event driventheory
nicky_walters
 
PPT
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
PPTX
Human Computer Interaction: User Interface Layer
achillescanonigo
 
PPT
Windows form applicationWindows form application
juraevmaruff
 
PDF
C# Summer course - Lecture 1
mohamedsamyali
 
PPT
introduction to_mfc
tuttukuttu
 
PPT
Gu iintro(java)
Satish Verma
 
PPT
Visual basic
sanjay joshi
 
PPT
Visual programming basic.ppt bs cs5th class
mnewg218
 
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Chapter 1
gebrsh
 
PDF
Winforms
Girmachew Tilahun
 
PDF
Delphi L03 Forms and Input
Mohammad Shaker
 
PPTX
Vb.net and .Net Framework
SHIVANGICHAURASIYA
 
PPT
Windows programming ppt
SAMIR CHANDRA
 
PPTX
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
VC++ Fundamentals
ranigiyer
 
Event handling in Java(part 1)
RAJITHARAMACHANDRAN1
 
UNIT 1 Overview of GUI programing-1.pptx
NarendraKamble8
 
LECTURE 12 WINDOWS FORMS PART 2.pptx
AOmaAli
 
Event driventheory
nicky_walters
 
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
Human Computer Interaction: User Interface Layer
achillescanonigo
 
Windows form applicationWindows form application
juraevmaruff
 
C# Summer course - Lecture 1
mohamedsamyali
 
introduction to_mfc
tuttukuttu
 
Gu iintro(java)
Satish Verma
 
Visual basic
sanjay joshi
 
Visual programming basic.ppt bs cs5th class
mnewg218
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Chapter 1
gebrsh
 
Delphi L03 Forms and Input
Mohammad Shaker
 
Vb.net and .Net Framework
SHIVANGICHAURASIYA
 
Windows programming ppt
SAMIR CHANDRA
 
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 

More from tadudemise (8)

PPTX
What is requirement gathering chap3 1.pptx
tadudemise
 
PPT
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
tadudemise
 
PPT
8329969.ppt
tadudemise
 
PPT
03a-IntroductionToEventDrivenProgramming.ppt
tadudemise
 
PPT
Introduction.ppt
tadudemise
 
PPT
Chapter 1.ppt
tadudemise
 
PPT
SPL_PS2 (1).ppt
tadudemise
 
PPT
03a-IntroductionToEventDrivenProgramming.ppt
tadudemise
 
What is requirement gathering chap3 1.pptx
tadudemise
 
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
tadudemise
 
8329969.ppt
tadudemise
 
03a-IntroductionToEventDrivenProgramming.ppt
tadudemise
 
Introduction.ppt
tadudemise
 
Chapter 1.ppt
tadudemise
 
SPL_PS2 (1).ppt
tadudemise
 
03a-IntroductionToEventDrivenProgramming.ppt
tadudemise
 
Ad

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
Designing Production-Ready AI Agents
Kunal Rai
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Ad

cse581_03_EventProgramming.ppt

  • 2. Window-based programming • Most modern desktop systems are window-based. Non-window based environment Window based environment What location do I use to set this pixel?
  • 3. Event-based Programming • Window-based GUI’s 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.
  • 4. Event-based Programming • Sample main programs: – C# / managed C++ static void Main() { Application.Run( new Form1()); } – C++ (MFC Document/View architecture) There isn’t one!! – GLUT void main(int argc, char** argv) { myInit(); glutMainLoop(); }
  • 5. Programming forms  Procedural programming  code is executed in sequential order.  Event-driven programming  code is executed upon activation of events. statement 1 statement 2 statement 3 -------- -------- statement n method 1 method 2 method 3 -------- -------- method n Do method 1 then method 3 Do method 2 then method 1 then method 3
  • 6. 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.
  • 7. 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
  • 8. General form of event- driven programming • The operating system and window system process input events from devices (mouse movement, mouse clicks, key presses etc) • The window system decides which window, and hence which frame and program, each input event belongs to.
  • 9. General form of event-driven programming • A data structure describing the event is produced and placed on an event queue. Events are added to one end of the queue by the window system. Events are removed from the queue and processed by the program. These event data structures hold information including: – Which of the program's windows this event belongs to. – The type of event (mouse click, key press, etc.) – Any event-specific data (mouse position, key code, etc.)
  • 10. Event loop • main(){ • ...set up application data structures... • ...set up GUI.... • // enter event loop • while(true){ • Event e = get_event(); • process_event(e); // event dispatch routine } } • the event loop is usually hidden from programmer, he/she provides just a process_event() method
  • 11. AWT Applications - Frame • Frame is a container for components Frame with normal window controls Optional Menu Three containers in Frame with Border Layout UI-components inside containers each with own layout
  • 12. Understanding the GUI • UI-containers – have list of UI-components • Each UI-component – is a class – with paint method – & lists of Event listeners {Frame} {label} {textfield} {button} components
  • 13. Understanding .NET UI Components • Overview – Replacement for VB Forms and MFC • Fully integrated with the .NET framework • Web Service aware • Data (ADO.NET and XML) aware • Secure code and Licensing – A framework for building Windows application • “No touch” deployment – No registration, Versionable, Smart caching – A RAD environment in Visual Studio .NET • Windows Form Designer, Property Browser
  • 14. Overview (cont) – Rich set of controls • Standard controls - Label, Button, TextBox, etc. • Common controls - ProgressBar, StatusBar, etc. • Data aware controls - ListBox, DataGrid, etc. • Common dialogs – 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
  • 15. Basic Terms • Component – Timer, DB Connection • Control (User Control) – Windows Forms • Menu • Button • etc – Web Forms • ASP.NET specific
  • 16. Using VS.NET • Adding Components and Controls – Just drag component from the toolbox • Editing properties – Edit directly in the properties editor • Registering for events – Also in the properties editor
  • 17. What actually happens? • Data members for components • For example, after placing timer: public class FunnyButton : System.Windows.Forms.Button { private System.Windows.Forms.Timer timer1;
  • 18. What actually happens? • Attributes for the properties • For example, after changing Text property: private void InitializeComponent() { … this.Text = "ADO Example";
  • 19. What actually happens? • Delegates for events – Special type event in .NET (special delegate) private void InitializeComponent() { … this->MouseEnter += new System.EventHandler(this.UserControl1_MouseEnter ); … } … private void UserControl1_MouseEnter(object sender, System.EventArgs e) {}
  • 20. Names everyone should know • Properties – Name – data member name – Text – text shown by control (was Caption) – ForeColor – current text color – Dock – docking to the parent – Enabled – active or not • Events – Click – mouse click on the control – SizeChanged - resize
  • 21. 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!