Event Driven Programming
Hawassa University Daye Campus
DEPARTMENT OF COMPUTER SCIENCE
Chapter Two
Introduction to .NET
Compiled by Talegeta G.
02/29/2024 Event Driven Programming with C# 1
Introduction to .NET
Event-driven programming is a programming paradigm in which the flow of the
program is determined by events
Event is an action or occurrence detected by a program; and originates from mainly two
sources:
Users:
mouse clicks, key presses
Another programs:
messages from other programs or threads
Event-driven program is designed to react such events
02/29/2024 Event Driven Programming with C# 2
Introduction to .NET…
In an event-driven application, there is generally a main loop that listens for events, and
triggers a callback function when one of those events is detected
A callback function, also called event handler, is a function that is invoked when an
event occurs
Start
Initialization
Wait for events
Y Exit or N
crash? Call event
Stop handler
Event
Handler
02/29/2024 Event Driven Programming with C# 3
Introduction to .NET…
Event-driven programming is the dominant paradigm used in graphical user interfaces
(GUIs)
A user interface is a system by which people (users) interact with the computer
There are two main types of user interfaces:
Text-Based User Interface or Command-Line Interface (CLI)
Graphical User Interface (GUI)
02/29/2024 Event Driven Programming with C# 4
Command Line Interface
This is a text only interface, which only requires input from the keyboard (usually
referred to as “entering a command”)
Typically, Enter key is pressed at the end of a command, after which the computer
will receive, parse and execute that command
The output of the command will be returned back to the terminal as text lines
This interface relies primarily on the keyboard
02/29/2024 Event Driven Programming with C# 5
Command Line Interface …
The main advantages of a CLI are:
• users only need to utilize the keyboard and may need to execute only few commands
to complete the task
• a computer that is only using the command line takes a lot less of the computers
resources
The main disadvantages of a CLI are:
• difficult for new users to learn
• relies heavily on recall rather than recognition
• navigation is often more difficult
02/29/2024 Event Driven Programming with C# 6
Graphical User Interface
It is a type of interface that allows users to communicate with applications in the form
of images/animations/icons as opposed to text
This interface presents the user with the information/actions through graphical objects
(i.e. GUI Controls or Widgets)
Both mouse and keyboard can be used for the interaction
The user performs actions by directly manipulating graphical objects on the screen
02/29/2024 Event Driven Programming with C# 7
Graphical User Interface….
The main advantages of a GUI are:
less expert knowledge is required to use it (more user friendly)
easier to navigate
The main disadvantages of a GUI are:
consumes system resources
slower compared to CLI
02/29/2024 Event Driven Programming with C# 8
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
02/29/2024 Event Driven Programming with C# 9
Cont…
.NET is a developer platform made up of tools, programming languages, and libraries
for building different types of applications
It is created by Microsoft
Its applications are multi-platform applications
It has been designed in such a way that it can be used from any of the following
languages: C#, C++, Visual Basic, etc
It consists of an enormous library of codes used by the client languages such as C#
using object-oriented programming (OOP) techniques
02/29/2024 Event Driven Programming with C# 10
.NET Framework …
C# Visual Basic C++
.NET Framework
Common Language Specification, Common Type System
WinForms ASP.NET WPF
ADO.NET
Framework Class Library
Common Language Runtime
02/29/2024 Event Driven Programming with C# 11
.NET Framework …
Common Language Runtime
The CLR is responsible for maintaining the execution of all applications developed
using the .NET library
It provides various services to managed code:
Compilation and execution
Integrating software components written in different .NET languages,
Garbage collection, and more.
02/29/2024 Event Driven Programming with C# 12
Common Language Runtime…
Managed code is compiled into machine-specific instructions in the following steps:
First, the code is compiled into Microsoft Intermediate Language (MSIL)
When the app executes, another compiler (known as the just-in-time compiler or JIT
compiler) in the CLR translates the MSIL in into machine-language code (for a
particular platform)
The machine-language code executes on that platform
02/29/2024 Event Driven Programming with C# 13
Common Language Runtime…
The Framework provides a high level of language interoperability
Because software components written in different .NET languages (such as C# and
Visual Basic) are all compiled into MSIL, the components can be combined to create a
single unified program
Thus, MSIL allows the .NET Framework to be language independent
02/29/2024 Event Driven Programming with C# 14
Framework Class Library
The .NET framework class library is a library of classes and interfaces that provide
access to system functionality
It is the foundation on which .NET Framework applications are built
The library is categorized into a number of namespaces
The System.Windows namespaces contain types used in Windows Presentation
Foundation (WPF) applications,
System.Windows.Forms and its child namespaces are used for developing Windows
Forms applications.
02/29/2024 Event Driven Programming with C# 15
ADO.NET
ADO.NET provides consistent access to data sources such as SQL Server and XML
Applications can use ADO.NET to connect to these data sources and perform CRUD
operations:
Create,
Read,
Update and
Delete data
02/29/2024 Event Driven Programming with C# 16
System.Windows (WPF)
System.Windows provides an object-oriented set of classes that enable you to develop
rich Windows applications
With this module and some additional code you can:
design UI
read information from the user
respond to the information provided
manipulate data from external sources (i.e. files and databases)
02/29/2024 Event Driven Programming with C# 17
Common Type System
The Common Type System (CTS) standardizes the data types of all
programming languages under the umbrella of .NET to a common data type
for easy and smooth communication
02/29/2024 Event Driven Programming with C# 18
Common Language Specification
CLS is a set of basic language features that .NET Languages needed to develop Applications and
Services , which are compatible with the .NET Framework.
When there is a situation to communicate Objects written in different .NET Complaint languages ,
those objects must expose the features that are common to all the languages
For example, one rule is that you cannot use multiple inheritance within .NET Framework. As you
may know C++ supports multiple inheritance but; that’s not allowed to use such a C++ code within
C# because it doesn’t supports multiple inheritance
Another rule is that you cannot have members with the same name but different cases i.e. you cannot
have add() and Add() methods. This easily works in C# because it is case-sensitive but because VB
is not case sensitive, it’s not possible to use such a code
02/29/2024 Event Driven Programming with C# 19
Common Language Specification…
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
02/29/2024 Event Driven Programming with C# 20
.NET Implementations
Three implementations:
.NET Framework is the original implementation of .NET. It supports running
websites, services, desktop apps, and more on Windows
.NET Core is a cross-platform implementation for running websites, services, and
console apps on Windows, Linux, and macOS. This implementation is open source
on GitHub
Xamarin/Mono is a .NET implementation for running apps on all the major mobile
operating systems, including iOS and Android
02/29/2024 Event Driven Programming with C# 21
.
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
> Mobile Apps
02/29/2024 Event Driven Programming with C# 22
.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.
02/29/2024 Event Driven Programming with C# 23
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
02/29/2024 Event Driven Programming with C# 24
Visual Studio …
ea
k ar
or
W
02/29/2024 Event Driven Programming with C# 25
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
02/29/2024 Event Driven Programming with C# 26
Familiarization with Visual Studio Project Types
Windows Projects
Store Apps
Web Projects
Office Projects
SharePoint Projects
Database Projects
02/29/2024 Event Driven Programming with C# 27
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
02/29/2024 Event Driven Programming with C# 28
Building Your First Application …
02/29/2024 Event Driven Programming with C# 29
Building Your First Application …
02/29/2024 Event Driven Programming with C# 30
Building Your First Application …
Console Application
Code Editor
02/29/2024 Event Driven Programming with C# 31
Building Your First Application …
Desktop Application
02/29/2024 Event Driven Programming with C# 32
Building Your First Application …
Program Code Form Design Code
02/29/2024 Event Driven Programming with C# 33
Using the intrinsic controls
Controls
Used to create user interface
TextBox
Label
Button
CheckBox
ListBox
ComboBox
ListView
…
02/29/2024 Event Driven Programming with C# 34
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, …
02/29/2024 Event Driven Programming with C# 35
.
Question!
?