You are on page 1of 60

Arba Minch University

Institute of Technology
Faculty of Computing & Software Engineering

Event Driven

Event Driven Programming


Programming
(SEngc351)
Mr. Addisu M. (Asst. Prof) G5 SE - Regular
1
Chapter 1

Event Driven Programming


Event Driven
Fundamentals
2
Outline
 What’s an event driven program?
 Working in the Programming Environment
 Building Your First Application
 Using the basic controls

Event Driven Programming


 Working with Properties, Methods, and
Events
 Working with Projects in event driven
Programming
3
What‘s an Event Driven Program?
 Event – is a type of signal to the program that something has
happened
 It is generated by external user actions such as mouse
mov’ts, mouse clicks, & key press, or by the OS, such as a
timer
 it act as a trigger to make program to do something

Event Driven Programming


 Event-driven programs respond to events from the computer,
such as the mouse button being pressed
 a programming paradigm in which the flow of the program is
determined by events
 Means: what a program does depends on what events have taken
place
 code is executed upon activation of events
 The designer uses ready-made objects such as Buttons and 4
What‘s an Event Driven Program?
 This approach to programming drastically reduces the
amount of code required to develop a Windows application
 Event driven program (such as Graphical User Interfaces
(GUIs) OS (windows), spreadsheet, etc) can be developed
using C# (C sharp), VB, java, java script, etc
 event-driven, meaning code remains idle until called upon

Event Driven Programming


to respond to some event (button pressing, menu selection,
…)
 It is governed by an event processor
 Nothing happens until an event is detected
 Once an event is detected, the code corresponding to that
event (event procedure) is executed
 Program control is then returned to the event processor 5
Working in Programming Env’t
 Programming env’t: a working platform which combines
hardware and software that allows developer to build applns
 Developers typically work in integrated deve’t env’ts or IDEs
 One of the IDE which can be used as a programming evn’t is
Visual Studio developed by Microsoft
 Visual Studio

Event Driven Programming


 offers a great deal of versatility, providing users with an expansive
library of extensions that allows for more customization than other
env’ts
 well-suited for in-depth projects
 But, it takes a lot of memory to open and run
 One of the programming language which is available for visual
studio IDE is C# (C Sharp) 6
Working in Programming Env’t
 C# is an OOP language provided by Microsoft that runs on
both .NET standards: .NET core and .NET Framework
 .NET core: open-source, general-purpose dev’t platform to build
cloud-based software applications on Windows, Linux, and macOS
 .NET Framework: software dev’t platform for building and running
Windows applications

Event Driven Programming


 C# - developed by Microsoft Corporation, USA
– New programming language that runs on the .NET Framework
– C# is simple, modern, type safe, and object oriented
– C# code is compiled as managed code
– Combines the best features of Visual Basic, C++ and Java
– Versionable
– Compatible
– Secure 7
Working in Programming Env’t
 Software framework is a universal, reusable software
platform used to develop applications, products & solutions,
includes:
 Support programs,
 Compilers,
 Code libraries,

Event Driven Programming


 An application programming interface (API) and
 Toolsets that bring together all the different components to
enable development of a project or solution
 A framework in software dev’t is a starting point, but you
add higher-level functionality to it to make it work
 serves as a foundation, so you’re not starting entirely
from scratch 8
Working in Programming Env’t
 Microsoft .NET is a Framework - language neutral
software which provides a common platform for building,
running and experiencing the next generation of
distributed applications
 Not a language

Event Driven Programming


 designed to fulfill the following objectives:
 Provide OOP environment
 Provide environment for developing various types of
applications, such as Windows-based applications and
Web-based applications
 To ensure that code based on the .NET Framework can
integrate with any other code. 9
Working in Programming Env’t
.NET Applications

Event Driven Programming


.NET Framework

Operating System + Hardware


10
.NET Framework Architecture

Visual Studio.NET
C++ VB C# JScript …

Common Language Specification

ASP.NET: Web Services Windows

Event Driven Programming


and Web Forms Forms

ADO.NET: Data and XML

Base Class Library

Common Language Runtime


11
…Conn,
• The .NET framework exposes numerous classes to the developer.
These classes allow the development of rich client applications and
Web based applications alike. In the above slide these classes have
been divided into 4 areas.
• ASP.NET provides the core Web infrastructure such as Web Forms
for UI based development and Web Services for programmatic

Event Driven Programming


interface development,
• User interface development on the Windows platform can be done
using Windows Forms
• ADO.NET and XML provide the functionality for data access.
• Finally, the core base classes provide infrastructure services such as
security, transaction management etc.
12
Conn,
• Common Language Runtime
 Common, secure execution environment.
• Windows forms
 Framework for building rich clients
• ASP.NET
 Web forms

Event Driven Programming


 Manageable code (non spaghetti)
 Logical evolution of ASP (compiled)
 Web Services
 Programming the Internet to leverage the "power at the edge of the cloud".
• ADO.NET, evolution of ADO
 New objects (e.g., DataSets, Datareader)
• Visual Studio.NET
 Most productive development environment gets better and fully supports 13
.NET Framework Architecture
1. Common Language Specification (CLS)
 contains guidelines, that language should follow so that they can
communicate with other .NET languages.
 responsible for Type matching (properly interoperate with other
2.languages)
Framework Base Class Libraries (BCL)
 collection of ready-made reusable classes that work with CLR

Intro to Distributed System


 consistent, object-oriented library of prepackaged functionality and
Applications
 Programmers using any of .NET languages can use these classes
 Examples of the classes provided by the library are:
 Database access and manipulation
 User Interface – Windows Forms, Web Forms, Web Services
 Security
 Encryption and decryption 14
.NET Framework Architecture
3. Common Language Runtime (CLR)
 language-neutral development & execution environment that
provides common runtime environment for all .NET languages
application execution
 common compiler for all language to one Machine Language.
 Intermediate Language (IL) to native code compiler.

Intro to Distributed System


 allows programmers to write code in different languages of their
choice, and ensure that the parts can work together
 provides programmers with services for memory management, data
types, security etc
 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. 15
.NET Framework Architecture
3. Common Language Runtime (CLR)
 Compilation in .NET
Code in another
Code in VB.NET Code in C# .NET Language

Intro to Distributed System


Appropriate
VB.NET compiler C# compiler Compiler

IL(Intermediate
Language) code

CLR just-in-time
execution 16
.NET Framework Architecture
3. Common Language Runtime (CLR)
 Intermediate Language (IL)
• .NET languages are not compiled to machine code
• compiled to an Intermediate Language (IL)
• CLR accepts IL code & recompiles it to the machine

Intro to Distributed System


code
• recompilation is just-in-time (JIT) meaning it is done
as soon as a function or subroutine is called
• JIT code stays in memory for subsequent calls
• In cases where there is not enough memory it is
discarded thus making JIT process interpretive 17
.NET Framework Architecture
Windows Forms
 Framework for building rich clients.
 Provides rapid appln dev’t env’t.
 Rich set of controls
 Data aware

Intro to Distributed System


 Printing support
 Unicode support
 UI inheritance
18
.NET Framework Architecture
ASP.NET
 Platform services that allow to program Web Applications
and Web Services in any .NET language.
 Uses .NET languages to generate HTML pages.
 ASP.NET “Program” is compiled into a .NET class and

Intro to Distributed System


cached the first time it is called. All subsequent calls use
the cached version.
 ASP.NET has two components
1. Web Forms: a UI allows clean cut code
2. Web Services: A programmable application component
accessible via standard Web protocols
19
.NET Framework Architecture
ADO.NET (Data and XML)
 ADO.NET is a set of classes that expose data access
services for .NET Framework programmers.
 ADO.NET provides a rich set of components for creating
distributed, data-sharing applications.

Intro to Distributed System


 ADO.NET provides consistent access to data sources such
as SQL Server and XML, and to data sources exposed
through OLE DB and ODBC.
 Data-sharing consumer applications can use ADO.NET to
connect to these data sources and retrieve, handle, and
update the data that they contain.
 Provides language neutral data access. 20
Uses of .NET Platform
 Provides a consistent object-oriented programming.
 Provides a code-execution environment that
 minimizes software deployment
 promotes safe execution of code

Event Driven Programming


 eliminates the performance problems of scripted or
interpreted environments.
 To make the developer experience consistent across
widely varying types of applications, such as
Windows-based and Web-based applications 21
Visual Studio.NET
 Development tool that contains a rich set of productivity
and debugging features.
 Types of applications we can create include:
 Console Application – Usually Text only application. Runs from
a DOS or COMMAND PROMPT screen.

Event Driven Programming


 Windows Application – Graphical Interface user application.
Typical Windows application that you normally use, such as MS
WORD etc.
 Web Based Windows Application – Web application that uses a
Browser such as Internet Explorer
22
Visual Studio Environment
 Integrated Development Environment (IDE)
 Form Designer
 Editor for entering code

Event Driven Programming


 Compiler
 Debugger
 Object Browser

23
Visual Studio IDE Start Page
Visual Studio Environment

Event Driven Programming


24
IDE New Project Dialog
Visual Studio Environment

Event Driven Programming


25
IDE Main Window Components
 Toolbars
 Document Window
 Form Designer
 Solution Explorer
 Properties Window

Event Driven Programming


 The Name property of a control is used to refer to the control in
code.
 The Text property holds the words that the user sees on the
screen.
 Toolbox
 Server Explorer,…
26
IDE Main Window Components

Event Driven Programming


27
IDE Main Window Components

Event Driven Programming


28
IDE Main Window Components
 Title bar: includes project name and icons to minimize,
maximize and close the project
 Menu bar: selecting one of the choices (File, Edit, View,
Project, Format,….Help) causes one of drop-down menus to
appear. The user can select required menu.
 Tool bar: icons on this line duplicate several of the more

Event Driven Programming


commonly used menu items
 acts as a short cut to select the menu
 Form design and project container windows: form design
window is where the user interface is actually designed by
placing the controls from Tool Box
 Each control can be moved, resized and its properties can be
assigned using properties window 29
IDE Main Window Components
 Properties window: each object has its own
unique list of properties
 Used to view or change the properties and
events of a selected control during design
 Press F4 or Select menu View > Properties
window

Event Driven Programming


 There are various options provided for viewing
the properties of the selected control, such as:
 Categorized view
 Alphabetical view
 Properties view (default)
 Events View
 Property pages
30
IDE Main Window Components
 Project Window: displays a hierarchical list of the files
associated with a given project
 Form layout window: specifies the screen location of the forms
within a project, the location can be changed by dragging the
form icon to the desired position
 Tool box: contains commonly used controls

Event Driven Programming


 control can be selected and placed in the Form by double
clicking on the control or by clicking once on the control, then
clicking on the desired location within the form
 associated code can be viewed or edited by double clicking on
the control

31
IDE Main Window Components
Toolbox
 Holds the controls you place on a form.
 There are various tool tabs available in the
Toolbox.
 controls in the IDE are presented in a hierarchical
manner (e.g., Standard Tab, Data Tab, Validation,

Event Driven Programming


Navigation, WebParts etc.).
 Depending on the type of project (application) the
toolbox tabs will vary.
 To view the tool box:
 Select menu View > Toolbox OR
 Press buttons Ctrl+Alt+X or Ctrl+W, X
 You can also view the controls as icons by right-
clicking on the toolbox you want to change and
deselecting the List View. 32
IDE Main Window Components
Solution Explorer
 Presents a tree view structure of files present in
the project
 To view Solution Explorer:
 Select View > Solution Explorer OR
 Press buttons Ctrl+W,S

Event Driven Programming


 Toolbar at the top of Solution Explorer enables
Code
various tasks. Refresh Copy Website

Properties View Designer


Nest Related
Files
ASP.NET
Configuration
33
Building Your First Application
 To develop C# projects, three steps process for planning the project
and then repeat the process for creating the project is to be done
Planning
 Design User Interface: Draw the forms and controls with the names
 Plan the Properties: specify the properties of all controls to set or change
during the design of the form
 Plan the basic code: plan the procedures to be executed when the project

Event Driven Programming


runs. Write the pseudo code, which is an English expression or comment
the part that describes the action
Programming:
 Define User Interface: create the forms and controls designed in the
planning stage
 Set the properties: define the properties of the controls
 Write the C# code: write C# programming statements to carry out the
actions needed 34
Building Your First Application
 C# programs consist of one or several files with a .cs extension,
which contain definitions of classes & other types
 These files are compiled by the C# compiler (csc) to executable
code and as a result assemblies are created, which are files with
the same name but with a different extension (.exe or .dll)

Event Driven Programming


 Example, if we compile HelloCSharp.cs, we will get a file with
the name HelloCSharp.exe and some additional files will be
created as well
 We can run the compiled code like any other program on our
computer by double clicking it
 If we try to execute the compiled code on a computer that does
not have .NET Framework, you will receive error message 35
Using the Intrinsic Controls
 In c#, forms are the foundations you generally use to build
programs
 A form is where you put all the things that people interact with
as they use your program
 Those things you put on the form as controls, which enable the
people who use your program to do things, such as enter text

Event Driven Programming


and click buttons
 Intrinsic Controls: are controls which are visible at the tool box
of visual studio
 intrinsic controls are available whenever you use c#
 Controls which are not visible on toolbox are called extrinsic
controls
 During design time, you can access them from the Toolbox 36
Using the Intrinsic Controls
 There are about 20 intrinsic controls which are built in to C#
 Every intrinsic controls are available to every C# program you
write
 When you create “New” project, all 20 of the intrinsic controls
will show up in Toolbox
 This is true for all versions of C#

Event Driven Programming


 The most commonly/pretty much used on every application.
Button CheckBox
Hyperlink Image
Label LinkButton
Panel Table
TableCell TableRow
TextBox 37
Adding Controls to Form
 Steps to create a Windows Form Application:
 Start graphically by dragging components from the Toolbox to the Form or
programmatically
 As components are added to the Form, Visual Studio assigns default names to
each one
 It is via these names that any C# code will interact with the user interface of

Event Driven Programming


the application
 name of these components can be changed in the Properties panel
 Adding Controls graphically by Using Windows Forms Designer
 Forms Designer provides Toolbox that contains variety of commonly used
controls
 drag and drop controls from Toolbox to form and arrange them as required
 activate the Toolbox by selecting View  Toolbox or press Ctrl+Alt+X
38
Adding Controls to Form
 Adding Controls graphically by Using Windows Forms Designer

Event Driven Programming


39
Adding Controls to Form
 Adding Controls graphically by Using Windows Forms Designer
 When adding a control to a form, it assumes a position based on where
the mouse landed when you clicked the form
 To move a control - specifying its position by changing its previous left
and top values either graphically or programmatically
 To move graphically

Event Driven Programming


 position the mouse on it until the cursor
changes into a cross
 click and drag left, right, up or down,
until you get to the desired location
 Using your keyboard, press either the
left, the up, the right, or the down arrow
keys to move the control until you get
the desired position
40
Adding Controls to Form
 Adding Controls graphically by Using Windows Forms Designer

 To move a control

Event Driven Programming


 position the mouse on the
control until the cursor
changes into cross
 blue lines that appear are
called snap lines
 help to align controls
 control can be selected to
delete it or move it

41
Adding Controls to Form
 Adding Controls graphically by Using Windows Forms Designer

:
ike
kl
l oo
ll
wi

Event Driven Programming


rm
fo

42
Adding Controls to Form
• Adding Controls Programmatically at run time
 involves more effort in finding the exact location where to
display the controls on the form than in plotting the controls
via the Windows Forms Designer
1. Create a new Windows Application project name it

Event Driven Programming


 Form1 is added to the project by default
2. Double-click Form1 to create and view
the Form1_Load event procedure
3. Add private variables to Form1 class to work with
common Window controls
43
Adding Controls to Form
• Adding Controls Programmatically at run time
public class Form1 : System.Windows.Forms.Form{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();

Event Driven Programming


private Label lblCount = new Label();
}
 IDE separates the source code into three separate files
 Form1.cs: the only one you edit (write your code)
 Form1.Designer.cs: Holds the auto-generated code that implements
all the actions that you performed by adding controls
 Program.cs: Contains the Main( ) method, where execution always
begins 44
Adding Controls to Form
• Adding Controls Programmatically at run time
//set up label for prompting Password
lblPassword = new Label();
lblPassword.Text = “Password: “;
lblPassword.Location = new Point(16, 80);

Event Driven Programming


//setup a text box that allows user to enter Name
txtName = new TextBox();
txtName.Location = new Point(152, 16);

//setup text box for entering password


txtPassword = new TextBox();
txtPassword.Location = new Point(152, 80);
txtPassword.PasswordChar = ‘*’;
45
Properties, Methods, & Events
Fields:
 are member variables of any type that is declared in the
class
 Example:
1. namespace CCharpChapterOne{

Event Driven Programming


2. public class Cat{
3. private string name; //field
4. private string color; //field
5. public static void Main(string[] args){
}
}
46
}
Properties, Methods, & Events
 Objects are described with “properties” (data) which tell
you what they look like, where they go, how they will act,
and what they contain
 Properties often describe physical properties of the object
(color or size), but they can also describe behavior, and the

Event Driven Programming


data contained by an object
 Examples of properties include:
 Appearance – color
 Behavior – dragging, visibility
 Position – top, left, width, height
 Content – text, graphics
 Font – name, size, bold, italic 47
Properties, Methods, & Events
 Object’s properties can be set in two ways
Design Time:
 Properties can be changed by typing a new value or choosing
from a list of predefined settings (available as a drop down list)
 A very important property for each object is its name.
 The name is used by C# to refer to a particular object in code

Event Driven Programming


Run Time:
 You can also set or modify properties while your application is
running. To do this, you must write some code.
 code format is: ObjectName.Property = NewValue
 Such a format is referred to as dot notation
 For example, to change the BackColor property of a form name
frmStart, we’d type: frmarStt.BackColor = BLUE 48
Properties, Methods, & Events

Event Driven Programming


49
Properties, Methods, & Events
 Objects usually have many things that they can do or that
can be done to them.
 It can solve a certain problem, eventually take parameters
and return a result.
 These are predefined procedures that often modify

Event Driven Programming


properties of the object such as its location
 For example:
 Move – change the location of an object
 Drag – allow it to be dragged with by the mouse
 Paint – display or redisplay the object
 Draw (Point, Line) – use the object to draw (e.g., use a
mouse) or draw with the object (e.g., a picture object) 50
Properties, Methods, & Events
 In the ff example, the text1.move will cause the text box to
move and be resized when the button is clicked

Event Driven Programming


51
Properties, Methods, & Events
 There are different types of methods in c#:
Call by value method:
 Value type parameters are passed a copy of the original value
 It doesn’t modify the original value
 A change made in passed value does not alter the actual value
 Example: call by value method:

Event Driven Programming


1. Class test{
2. Static void Change(int a){
3. a = 4; Console.WriterLine(a); }
4. Static void Main(string[] args){
5. int n = 12;
6. Change(n); //pass copy of n to change method not n’s
address
7. Console.WriteLine(n); } 52
Properties, Methods, & Events
 There are different types of methods in c#:
Reference method:
 Example: reference method:
1. Class test{
2. Static void Change(ref int a){
3. a = 4; Console.WriteLine(a); }

Event Driven Programming


4. Static void Main(string[] args){
5. int n = 12;
6. Change(ref n);
7. Console.WriteLine(n); }
}
 ref keyword is used to pass and return the reference value
 Any value change that passed as a reference also changes 53
Properties, Methods, & Events
 Events are triggered when windows are “loaded”, buttons
are “clicked”, or text boxes are “changed”
 For example: Click, Double Click, Mouse Down, Over, Up,
drag change
 In many programming systems such as C#, objects are

Event Driven Programming


associated with behavior by appending a descriptive
behavioral term to the name of the object
 Example: “btn_click” indicating that the button has been clicked
 In some cases, certain events such as “onClick” are
explicitly associated with the object, a button
 Code is attached to events to drive a program
54
Properties, Methods, & Events

Event Driven Programming


55
Properties, Methods, & Events
Name Description
Click Occurs when a control is clicked. In some cases, this event will also
occur when a user presses Enter
DoubleClick Occurs when a control is double-clicked. Handling the Click event
on some controls, such as the Button control will mean that the
DoubleClick event can never be called
DragDrop Occurs when a drag-and-drop operation is completed, in other

Event Driven Programming


words, when an object has been dragged over the control, and the
user releases the mouse button
DragEnter Occurs when an object being dragged enters the bounds of the
control
DragLeave Occurs when an object being dragged leaves the bounds of the
control
DragOver Occurs when an object has been dragged over the control.
56
Properties, Methods, & Events
Name Description
KeyDown Occurs when a key becomes pressed while the control has focus. This event
always occurs before KeyPress and KeyUp.
KeyPress Occurs when a key becomes pressed, means the event always occurs after
KeyDown and before KeyUp. The difference between KeyDown and
KeyPress is that KeyDown passes the keyboard code of the key that has

Event Driven Programming


been pressed, while KeyPress passes the corresponding char value for the
key
KeyUp Occurs when a key is released while a control has focus. This event always
occurs after KeyDown and KeyPress.
GotFocus Occurs when a control receives focus. Do not use this event to perform
validation of controls
LostFocus Occurs when a control looses focus. Do not use this event to perform
validation of controls
57
Properties, Methods, & Events
Name Description
MouseDown Occurs when the mouse pointer is over a control and a mouse button is
pressed. This is not the same as Click event because MouseDown occurs
as soon as the button is pressed and before it is released

Event Driven Programming


MouseMove Occurs continually as the mouse travels over the control.
MouseUp Occurs when the mouse pointer is over a control and a mouse button is
released
Paint Occurs when the control is drawn.
Validating Fires when a control with the CausesValidation property set to true is
about to receive focus. Note that the control which is to be validated is
the control which is losing focus, not the one that is receiving it.

58
Properties, Methods, & Events
Events Procedures
 Usually, nothing happens until the user does something and
generates an event
 Syntax:
private return_type objectName_event(...) {

Event Driven Programming


statements
}

Example:
private void btnShow_Click(object sender, EventArgs e){
int mark = int.Parse(txtMark.Text);
} 59
The End!
Questions, Ambiguities,

Event Driven Programming


Doubts, … ???
60

You might also like