Event Driven Programming
Fundamentals of Event Driven Programming
Chalew Tesfaye
Department of Computer Science
Debre Berhan University
2016
Objectives
At the end of the lesson students should be able to:
Understand the process of software development
Understand the concept of event driven programming
Understand the .NET framework and its structure
Identify the components of Visual Studio IDE
Build C# project using Visual Studio IDE
Debug C# Project
12/25/2020 Event Driven Programming with C# 2
Contents
Software development process
Fundamentals of Event Driven Programming
What’s an event driven program?
Introduction to .NET framework
Working in the Integrated Development Environment
Building Your First Application in Visual studio 2013
Debugging Your Applications
12/25/2020 Event Driven Programming with C# 3
Event Driven Programming
Event
things that happen
For example,
> you click a button, and the program does something interesting.
Event driven programming
is a programming paradigm in which the flow of program execution is
determined by events
For example a user action such as a mouse click, key press, or a message
form the operating system or another program
An event-driven application is designed to
detect events as they occur, and then
deal with them using an appropriate event-handling procedure
12/25/2020 Event Driven Programming with C# 4
Event …
Event driven, code-behind programming…
Event source
Event
> individual user actions are translated into “events”
Event listener
Event handler
> events are passed, 1 by 1, to application for processing
A visual programming IDE such as MS Visual Studio
provides much of the code for detecting events and
Wires event listener to event handler automatically when a new application created
The programmer concentrates on
User Interface design
Writing the event handler/the code
12/25/2020 Event Driven Programming with C# 5
Event …
Structural Development >
Procedural Programming
> Event handlers implemented as subroutines
> The flow of program execution was
determined by the programmer, and
controlled from within the application’s main routine.
Modern event-driven program,
there is no visible flow of control
The main routine is an event-loop that
> waits for an event to occur, and then
> invokes the appropriate event-handling routine
Since the code for this event loop is
> usually provided by the event-driven development environment or framework
> largely invisible to the programmer, application is a collection of event handler routines
Accelerated by the introduction of GUI
12/25/2020 Event Driven Programming with C# 6
Event …
OO Development >
OO Programming
> Object
Real world entity
> Class
is an object template
defines the
• attributes,
• methods and The relationship between an object and an
application
• events that will be implemented in any
object created with it. Credit: www.technologyuk.net
> An object is an instance of a class
12/25/2020 Event Driven Programming with C# 7
How event-driven programming works?
The central element of an event-driven application is
a scheduler/listener
> that receives a stream of events and
> passes each event to the relevant event-handler
continue to remain active until it encounters an event
> e.g. "End_Program“ - that causes it to terminate the application
Event includes
actions performed by the user during the execution of a program
> Click event, Key press event, Speak event, Touch event, …
messages generated by the operating system or another application
an interrupt generated by a peripheral device or system hardware
Timely generated message
Message sent via network from other system
The events are dealt with by a central event-handler (usually called a dispatcher or scheduler or listener)
that runs continuously in the background and waits for an even to occur.
When an event does occur,
the scheduler/ listener must determine the type of event and call the appropriate event-handler to deal with it.
The information passed to the event handler by the scheduler will vary, but will include sufficient information
to allow the event-handler to take any action necessarily.
12/25/2020 Event Driven Programming with C# 8
How event …
do forever: // the main scheduler loop
get event from input stream
if event type == EndProgram:
quit // break out of event loop
else if event type == event_01:
call event-handler for event_01 with event parameters
else if event type == event_02:
call event-handler for event_02 with event parameters
.
.
.
A simple event-driven programming paradigm
else if event type == event_nn: Credit: www.technologyuk.net
call event-handler for event_nn with event parameters
else handle unrecognized event // ignore or raise exception
end loop
12/25/2020 Event Driven Programming with C# 9
Event-driven programming Language
Almost all object-oriented and visual languages support event-driven programming
C#, Visual Basic, Visual C++ and Java are examples of such languages.
A visual programming IDE such as Visual Studio
provides much of the code for detecting events automatically when a new application is created
In this course we use C#, to develop event driven program
C#
pronounced as see sharp
is a multi-paradigm programming language
enables developers to build a variety of secure and robust applications that run on the .NET Framework
Can used to create
> Windows client applications,
> Windows Store apps
> XML Web services,
> distributed components,
> client-server applications,
> database applications, and much, much more.
12/25/2020 Event Driven Programming with C# 10
Introduction to .NET framework
.NET is
pronounced as “dot net”
a software framework that runs primarily on Microsoft Windows.
provides a common platform to Execute or, Run the applications developed in
various programming languages.
Microsoft announced the .NET initiative in July 2000.
The main intention was to bridge the gap in interoperability between services of
various programming languages
The .NET Framework is designed to fulfill the following objectives:
Provide object-oriented programming environment
Provide environment for developing various types of applications, such as Windows-
based applications, Web-based applications, Windows 8 app, Mobile app
To ensure that code based on the .NET Framework can integrate with any other code
12/25/2020 Event Driven Programming with C# 11
.NET Framework …
VB C++ C# JScript …
Common Language Specification
Visual Studio 2013
Windows
ASP.NET ADO.NET
Forms
Mobile App Windows 8 App
(CLR) Common Language Runtime
Operating System
12/25/2020 Event Driven Programming with C# 12
.NET Framework …
The .NET Framework consists of:
The Common Language Specification (CLS)
> contains guidelines, that language should follow so that they can communicate
with other .NET languages
> responsible for Type matching.
The Framework Base Class Libraries (BCL)
> a consistent, object-oriented library of prepackaged functionality and
Applications.
The Common Language Runtime (CLR)
> A language-neutral development & execution environment that provides
common runtime for application execution .
12/25/2020 Event Driven Programming with C# 13
.NET Framework …
Common Language Specification (CLS) performs the following
functions:
Establishes a framework that helps enable
> cross-language integration,
> type safety, and
> high performance code execution
Provides an object-oriented model
> that supports the complete implementation of many programming languages
Defines rules that languages must follow,
> which helps ensure that objects written in different languages can interact with
each other
12/25/2020 Event Driven Programming with C# 14
.NET Framework …
.NET Framework Base Class Library (FBC)
The Class Library is a comprehensive, object-oriented collection of
reusable types
These class library can be used to develop applications that include:
> Traditional command-line applications
> Graphical user interface (GUI) applications
> Applications based on the latest innovations provided by ASP.NET
Web Forms
XML Web services
> Windows 8 Apps
Unified Classes
> Mobile Apps Mobile App
Web Classes
Classes
Windows Windows 8
Data Classes
Form Classes Classes
12/25/2020 Event Driven Programming with C# 15
.NET Framework …
Common Language Runtime (CLR) ensures:
> A common runtime environment for all .NET languages
> Uses Common Type System (strict-type & code-verification)
> Memory allocation and garbage collection
> Intermediate Language (IL) to native code compiler. Which Compiles MSIL code
into native executable code
> Security and interoperability of the code with other languages
Over 36 languages supported today
> C#, VB, Jscript, Visual C++ from Microsoft
> Perl, Python, Smalltalk, Cobol, Haskell, Mercury, Eiffel, Oberon, Oz, Pascal, APL,
CAML, Scheme, etc.
12/25/2020 Event Driven Programming with C# 16
.NET Framework …
Execution in CLR
Source
VB C# C++ ...
code
Compiler Compiler Compiler
.Net compatible language
Managed CLS compile to a second
platform-neutral
code Assembly Assembly Assembly language called Common
IL Code IL Code IL Code Intermediate Language
The platform specific CLR
Common Language Runtime compiles CIL to machine
readable code that can
JIT Compiler be executed on the
current platform
Native Code
Operating System Services
12/25/2020 Event Driven Programming with C# 17
.NET Framework …
Comparison to Java
compile execute
Hello.java Hello.class JVM
Source code Byte code
Hello.cs compile Hello.exe execute CLR
Source code CIL
12/25/2020 Event Driven Programming with C# 18
Visual Studio IDE
Microsoft has introduced Visual Studio IDE
which is a set of tool in a single application for developing .NET applications by
using
> programming languages such as VB, C#, VC++ and VJ#, etc.
used to create applications such as Windows Store apps, Windows desktop
apps, console apps, Web apps, and Windows Phone apps
Visual Studio provides
Automatic skeleton code generation
Rapid coding experience
Everything at your fingertips
> Different tools, easily navigability
Customizability and
Extensibility
12/25/2020 Event Driven Programming with C# 19
Visual Studio …
ea
k ar
or
W
12/25/2020 Event Driven Programming with C# 20
Navigating the Visual Studio Environment
The Menu
The menu bar is a standard part of most windows applications.
“File,” “Edit,” “View,” “Tools,” and so on.
Toolbar
contains frequently accessed functionality that is a subset of what is available via menus
Work area
use to write code and work with visual designers
Toolbox
contains a context sensitive list of controls that can be dragged and dropped onto the current designer
surface
Solution Explorer
is where your solutions, projects, and project items will appear
Status Bar
communicates what is happening with VS at the current time
Much more
12/25/2020 Event Driven Programming with C# 21
Managing Visual Studio Windows
Expanding and Collapsing
Windows
Docking Windows
Floating Windows
Tabbed Windows
Closing and Opening
Windows
Resetting All Settings
12/25/2020 Event Driven Programming with C# 22
Familiarization with Visual Studio Project Types
Windows Projects
Store Apps
Web Projects
Office Projects
SharePoint Projects
Database Projects
12/25/2020 Event Driven Programming with C# 23
Building Your First Application in VS 2013
Start Visual Studio 2013
New Project
Select project template
Select project
The most common applications are:
Windows Form Application
Console Application
WPF Application
Windows Store App
ASP.NET Web Application
Silverlight Application
Provide project name, location
12/25/2020 Event Driven Programming with C# 24
Building Your First Application …
12/25/2020 Event Driven Programming with C# 25
Building Your First Application …
12/25/2020 Event Driven Programming with C# 26
Building Your First Application …
Console Application
Code Editor
12/25/2020 Event Driven Programming with C# 27
Building Your First Application …
Desktop Application
12/25/2020 Event Driven Programming with C# 28
Building Your First Application …
Program Code Form Design Code
12/25/2020 Event Driven Programming with C# 29
Using the intrinsic controls
Controls
Used to create user interface
TextBox
Label
Button
CheckBox
ListBox
ComboBox
ListView
…
12/25/2020 Event Driven Programming with C# 30
Methods, Properties, and Events
Methods
Are extremely useful because they allow you to separate your logic into different units
Are similar to functions, procedure or subroutine used in other programming languages
The difference is that a method is always a part of a class
Properties
Attribute of an object
Example
> TextBox – Color, Font Family, Font Size
Events
An action the object responds
Example
> TextBox – TextChanged Event
> Button – Click event, …
12/25/2020 Event Driven Programming with C# 31
Debugging Your Applications
In Visual Studio we have lots of
Build and Debugging Tools
Build menu
The most used tool is “Build Solution”
12/25/2020 Event Driven Programming with C# 32
Debugging …
Debug menu:
The most used tool is “Start
Debugging” (Shortcut Key: F5)
12/25/2020 Event Driven Programming with C# 33
Debugging …
Console Program
12/25/2020 Event Driven Programming with C# 34
Debugging …
Desktop program
12/25/2020 Event Driven Programming with C# 35
More information on
John Sharp. Microsoft Visual C# 2013 Step by Step, 2015
Microsoft Press USA
Joel Murach, Anne Boehm. Murach C# 2012, Mike Murach &
Associates Inc USA, 2013
Lalit Arora. .Net framework with C# programming, 2007 India (HC
available in your library)
http://
www.technologyuk.net/computing/software_development/event
_driven_programming.shtml
, accessed on date 2/18/2016
Microsoft Corporation. Microsoft Visual Studio 2012 Product
Guide, 2012
12/25/2020 Event Driven Programming with C# 36
Thank You!
?