SlideShare a Scribd company logo
1 of 21
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 Event-Based Programming Explained

Advanced Coded UI Testing
Advanced Coded UI TestingAdvanced Coded UI Testing
Advanced Coded UI TestingShai Raiten
 
Implementation
ImplementationImplementation
Implementationhcicourse
 
Lesson 2 - Understanding Operating System Configurations
Lesson 2 - Understanding Operating System ConfigurationsLesson 2 - Understanding Operating System Configurations
Lesson 2 - Understanding Operating System ConfigurationsGene Carboni
 
Windows programming ppt
Windows programming pptWindows programming ppt
Windows programming pptSAMIR CHANDRA
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
A look behind the scenes: Windows 8 background processing
A look behind the scenes: Windows 8 background processingA look behind the scenes: Windows 8 background processing
A look behind the scenes: Windows 8 background processingMatt Lacey
 
3 App Compat Win7
3 App Compat Win73 App Compat Win7
3 App Compat Win7llangit
 
Event driven theory
Event driven theoryEvent driven theory
Event driven theorynickywalters
 
Introduction to embedded system
Introduction to embedded systemIntroduction to embedded system
Introduction to embedded systemajitsaraf123
 
Intro To webOS
Intro To webOSIntro To webOS
Intro To webOSfpatton
 
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)Codit
 
Debugging IE Performance Issues with xperf, ETW and NavigationTiming
Debugging IE Performance Issues with xperf, ETW and NavigationTimingDebugging IE Performance Issues with xperf, ETW and NavigationTiming
Debugging IE Performance Issues with xperf, ETW and NavigationTimingNicholas Jansma
 
ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017Lucas Jellema
 
Understanding &Troubleshooting the Windows Logon Process
Understanding &Troubleshooting the Windows Logon ProcessUnderstanding &Troubleshooting the Windows Logon Process
Understanding &Troubleshooting the Windows Logon ProcessControlUp
 

Similar to Event-Based Programming Explained (20)

Advanced Coded UI Testing
Advanced Coded UI TestingAdvanced Coded UI Testing
Advanced Coded UI Testing
 
c programming 1-1.pptx
c programming 1-1.pptxc programming 1-1.pptx
c programming 1-1.pptx
 
Implementation
ImplementationImplementation
Implementation
 
Lesson 2 - Understanding Operating System Configurations
Lesson 2 - Understanding Operating System ConfigurationsLesson 2 - Understanding Operating System Configurations
Lesson 2 - Understanding Operating System Configurations
 
Gui
GuiGui
Gui
 
Windows programming ppt
Windows programming pptWindows programming ppt
Windows programming ppt
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
A look behind the scenes: Windows 8 background processing
A look behind the scenes: Windows 8 background processingA look behind the scenes: Windows 8 background processing
A look behind the scenes: Windows 8 background processing
 
3 App Compat Win7
3 App Compat Win73 App Compat Win7
3 App Compat Win7
 
Visual basic
Visual basic Visual basic
Visual basic
 
2310 b 05
2310 b 052310 b 05
2310 b 05
 
Event driven theory
Event driven theoryEvent driven theory
Event driven theory
 
Introduction to embedded system
Introduction to embedded systemIntroduction to embedded system
Introduction to embedded system
 
Intro To webOS
Intro To webOSIntro To webOS
Intro To webOS
 
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)
Azure Service Fabric: notes from the field (Sam Vanhoute @Integrate 2016)
 
Debugging IE Performance Issues with xperf, ETW and NavigationTiming
Debugging IE Performance Issues with xperf, ETW and NavigationTimingDebugging IE Performance Issues with xperf, ETW and NavigationTiming
Debugging IE Performance Issues with xperf, ETW and NavigationTiming
 
ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017
 
Unit 4 software
Unit 4  softwareUnit 4  software
Unit 4 software
 
Understanding &Troubleshooting the Windows Logon Process
Understanding &Troubleshooting the Windows Logon ProcessUnderstanding &Troubleshooting the Windows Logon Process
Understanding &Troubleshooting the Windows Logon Process
 
7java Events
7java Events7java Events
7java Events
 

More from tadudemise

What is requirement gathering chap3 1.pptx
What is requirement gathering chap3 1.pptxWhat is requirement gathering chap3 1.pptx
What is requirement gathering chap3 1.pptxtadudemise
 
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
chaptertaaaaaaaaaaaaaaadddddddd2222 4.pptchaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppttadudemise
 
03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppttadudemise
 
Introduction.ppt
Introduction.pptIntroduction.ppt
Introduction.ppttadudemise
 
SPL_PS2 (1).ppt
SPL_PS2 (1).pptSPL_PS2 (1).ppt
SPL_PS2 (1).ppttadudemise
 
03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppttadudemise
 

More from tadudemise (8)

What is requirement gathering chap3 1.pptx
What is requirement gathering chap3 1.pptxWhat is requirement gathering chap3 1.pptx
What is requirement gathering chap3 1.pptx
 
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
chaptertaaaaaaaaaaaaaaadddddddd2222 4.pptchaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
chaptertaaaaaaaaaaaaaaadddddddd2222 4.ppt
 
8329969.ppt
8329969.ppt8329969.ppt
8329969.ppt
 
03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt
 
Introduction.ppt
Introduction.pptIntroduction.ppt
Introduction.ppt
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
SPL_PS2 (1).ppt
SPL_PS2 (1).pptSPL_PS2 (1).ppt
SPL_PS2 (1).ppt
 
03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt03a-IntroductionToEventDrivenProgramming.ppt
03a-IntroductionToEventDrivenProgramming.ppt
 

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Event-Based Programming Explained

  • 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!