Event Driven Programming
Chapter one
Introduction
Agenda
• What's event driven programming?
– C#
– .Net framework
• Visual Studio 2008,2010,2015 etc
• Building Your First Application
• Using the intrinsic controls
• Working with Properties
• Consol applications
30/09/2024 Event-driven Programming 2
What is event driven programming?
• Before Defining event drive programming, we must
first know
– what events are?
• Events are the actions that are performed by the user
during the application usage.
• Examples
– Clicking a mouse button
– moving the mouse
– Pressing a Key down/up
– Encountering messages from other programs or threads
30/09/2024 Event-driven Programming 3
Event driver programming…?
• Event driver programming (which is also called event-
based programming ) is any programming language,
which uses these events to run a specific portion of the
program.
• Other definitions
– It is a programming paradigm in which the flow of the
program is determined by events.
– Event-driven programming is an application architecture
technique in which the application has a main loop which
is clearly divided down to two sections:
• the first is event selection (or event detection), and
• the second is event handling
30/09/2024 Event-driven Programming 4
Event driver programming…?
• In EDP, the program waits for events to occur and
then responds to them using pieces of code called
event handlers.
Key Concepts
1. Event:
Something that happens — e.g., clicking a button, typing text, closing a
window.
2. Event Source:
The object that generates the event (e.g., a Button control).
3. Event Handler:
A function or method that executes in response to an event (e.g.,
button1_Click()).
4. Event Loop:
Keeps listening for events and dispatching them to the appropriate handlers.
30/09/2024 Event-driven Programming 5
Event driver programming…
• Games are the biggest example of these kinds of
applications.
– The GUI based programs are all developed using event
driven programming.
• The other technology used in contrast to event driver
programs is command driven programs, where using
the command driven interface a user does each and
every task.
– But this programming paradigm is difficult to operate and
is not user friendly.
• The commands must be remembered in order to work it possible.
30/09/2024 Event-driven Programming 6
Event driver programming…
• The event driven programs are very user friendly
and highly interactive systems.
• Event-driven programs can be written in any
language, although the task is easier in languages
that provide high-level abstractions.
• The entire object based languages and visual
languages supports the event driver
programming.
– Visual Basic, Visual FoxPro, JavaScript, Visual C++ ,
visual [Link], C# and Java are the examples of
these languages.
30/09/2024 Event-driven Programming 7
Introduction to C# and .NET
Framework
• C# (C-Sharp):
– Modern, object-oriented programming language developed by
Microsoft.
– Designed for building Windows applications, web apps, and
enterprise systems.
– Combines power of C++ with simplicity of Visual Basic.
• .NET Framework:
– A software framework from Microsoft for building and running
applications on Windows.
– Provides a runtime environment and class libraries for
developers.
– Supports multiple languages
30/09/2024
(C#, [Link], F#).
Event-driven Programming 8
Introduction to C# and .NET
Framework
• Key Components:
• CLR (Common
LanguageRuntime): [Link] programs.
• BCL (Base Class
Library): Providesreusableclasses (I/O,
networking, collections,etc.).
30/09/2024 Event-driven Programming 9
Development Environment-Visual
Studio
• Visual Studio (2008, 2010, 2015, etc.):
• Integrated Development Environment (IDE) for building .NET
applications.
• Features:
– Code Editor with IntelliSense
– Form Designer for GUI
– Debugger for testing
– Toolbox for drag-and-drop controls
– Common Project Types:
• Console Applications
• Windows Forms Applications
• Web Applications ([Link])
• Event-driven GUIs: Built using Windows Forms where actions
(clicks, typing, etc.) trigger events.
30/09/2024 Event-driven Programming 10
Building Your First C# Application
Steps in Visual Studio:
1. Open Visual Studio → Create New
• Example-Simple Console Project → Console App (.NET).
App: 2. Write code in [Link].
using System; 3. Press Run (F5) to execute.
Key Concepts:
• Namespace: Organizes code
class HelloWorld { (System, MyApp).
static void Main() { • Class: Defines program structure.
• Main(): Entry point of execution.
[Link]("Hello, C#.NET • Console Class: Used for
World!"); input/output.
[Link]();
}
}
30/09/2024 Event-driven Programming 11
What is .NET framework
• The .NET Framework is a software development platform
created by Microsoft for building and running applications on
Windows.
• It is a software framework that can be installed on computers
running Ms windows operating systems.
• It provides an unprecedented platform for building Windows,
web, and mobile applications with one or more languages.
It provides a controlled programming environment where
software can be developed, installed, and executed.
30/09/2024 Event-driven Programming 12
What is .NET framework
• Main Components
• CLR (Common Language Runtime):
– The engine that runs .NET applications.
– Handles memory management, exception handling, security, and
garbage collection.
• BCL (Base Class Library):
– A large collection of pre-built classes and functions (for file handling,
strings, data, networking, etc.).
– Helps developers write code faster and more securely.
• Languages Supported:
– C#, [Link], F#, and more — all compiled into Microsoft Intermediate
Language (MSIL).
• [Link]:
– A part of .NET used for web application development.
30/09/2024 Event-driven Programming 13
What is .NET framework…
• The principal design features are:
– Interoperability: This allows for .NET-developed programs to
access functionalities in programs developed outside .NET.
– Common Runtime Engine: Also known as the common language
runtime, this allows programs developed in .NET to exhibit
common behaviors in memory usage, exception handling and
security.
– Language Independence: Common language infrastructure
specifications (CLI) allow for the exchange of data types
between two programs developed in different languages.
– Base Class Library: A library of code for most common
functions--used by programmers to avoid repetitive rewriting of
code.
– Ease of Deployment: There are tools to ensure the ease of
installing programs without interfering with previously installed
applications.
– Security: Programs developed in .NET are based on a common
30/09/2024security model. Event-driven Programming 14
The .NET Framework
VB C++ C# JScript …
Common Language Specification
Visual [Link]
[Link]: Web Services Windows
Windows
and Web Forms Forms
Forms
[Link]: Data and XML
Base Class Library
Common Language Runtime
30/09/2024 Event-driven Programming 15
What is .NET framework
• .NET has been a success in many respects.
• Within the .NET Framework
– new languages (C# and J#) have been born and
– the well - established Visual Basic language has
been reborn.
• The .NET Framework even supports legacy
languages such as C++.
30/09/2024 Event-driven Programming 16
30/09/2024 Event-driven Programming 17
Visual Studio
• Visual Studio .NET is Microsoft’s Integrated
Development Environment (IDE) for creating,
running and debugging programs (also called
applications) written in a variety of .NET
programming languages.
• This IDE is a powerful and sophisticated tool for
creating business critical and mission-critical
applications.
• Prior to beginning C#.NET installation, you should
make some preliminary checks first.
– You must verify that you meet the system requirements
for installation.
– Platform
30/09/2024 Event-driven Programming 18
Visual Studio …
• When you install Visual Studio .NET, you will be
asked to install the MSDN for Visual Studio .NET,
which contains valuable information on .NET
development.
• You can also install sample projects that help you
learn .NET.
• If you aren’t sure whether you need a component
during installation, you can always add
components later.
• When Visual Studio .NET begins execution, the
Start Page displays
30/09/2024 Event-driven Programming 19
The Main Windows of the Visual Studio IDE
30/09/2024 Event-driven Programming 20
Building Your First Application
• You have to create a project first to work on
C#.NET programming.
• To create a project you have three options
once you are in main window
– Click File → New Project
– Click on New Project button on the tool bar
– Press CTRL + New Keys
• The following Window will appear
subsequently
30/09/2024 Event-driven Programming 21
New Project Window
30/09/2024 Event-driven Programming 22
Building Your First Application…
• From the window displayed choose windows
Form applications template.
– Using [Link] different kinds of templates such as,
applications or libraries or even empty project can be
created.
• If you look in the Name textbox at the bottom of
the above figure , you'll see it says
"WindowsApplication1".
– This is the default name for your projects. It's not a
good idea to keep this name.
• So, click inside this textbox and change this Name
to what ever name you want to give for your
project (MyNewProject , may be)
30/09/2024 Event-driven Programming 23
Building Your First Application…
• Keep the Location the same as the default. This is
a folder inside of your "My Documents" folder
called "Visual Studio Projects".
• A new folder will then be created for you, and its
name will be the one you typed in the "Name"
textbox.
• All of your files for your first project are then
saved in this folder.
• Click the OK button, and the Visual Basic .NET
design time environment will open. It will look
like the following
30/09/2024 Event-driven Programming 24
C#.NET design time environment
30/09/2024 Event-driven Programming 25
C#.NET Forms
• A Big square in the top left corner of the
design environment is called Form.
– All the controls for an application are assembled
on forms.
30/09/2024 Event-driven Programming 26
Adding Controls Using the Toolbox
• Things like buttons, textboxes, and labels are
all things that you can add to your Forms.
• They are know as Controls, and are kept in the
Toolbox for ease of use.
• The Toolbox can be found on the toolbox icon
next to Form1:
30/09/2024 Event-driven Programming 27
Adding Controls Using the Toolbox
• To display all the tools, move your mouse over
the toolbox icon. You'll see the following
automatically appear:
• There are seven categories of
tools available. The toolbox
you'll be working with first is the
Common Controls toolbox.
• To see the tools, click on the
plus symbol next to Common
Controls. You'll see a long list of
tools:
30/09/2024 Event-driven Programming 28
30/09/2024 Event-driven Programming 29
Adding Controls Using the Toolbox..
• As you can see, there are a lot of tools to
choose from!
• For this first section, we'll only be using a
Button.
• If you want to keep the toolbox displayed,
click the Pin icon next to the X.
• To close the toolbox, simply move your mouse
away.
30/09/2024 Event-driven Programming 30
Adding a Tool (Control) to your Form
• Let's start by adding a button to our form. With
the tools displayed, do the following:
– Locate the Button tool (from common controls)
– Double click the icon ( or single click on the button
and then draw the button on the form)
– A Button is added to your form
• The textbox gets added to the top left position of
your form (with Button1 displayed on it).
– We will see later how to change this name
• To move it down, hold your mouse over the
button and drag to a new position.
30/09/2024 Event-driven Programming 31
Adding a Tool (Control) to your Form
• The small squares around the button are
sizing handles.
• Move your mouse over one of them. The
mouse pointer turns into an extended line
with arrowheads.
• Hold your left mouse button down and drag
outwards. The button is resized.
30/09/2024 Event-driven Programming 32
Adding a control to your Form :
Option 2
• With your Form displayed in the Visual Basic
Design environment:
– Click on the Button tool in the toolbox with the left
hand mouse button, but click only once
– Move your mouse to a blank area of your form - the
mouse pointer will turn into a cross
– Press and hold down the left mouse button
– Drag across the form with the button held down
– Let go of the mouse button when you're happy with
the size
– A Button is drawn
30/09/2024 Event-driven Programming 33
Changing button Control in C#.NET
form
Move to left bottom corner and rewrite button1
to ‘Ok’
30/09/2024 Event-driven Programming 34
Adding a code for a Button1 control
• To get our first look at the code window, double click
your Button control. The code window will appear, and
will look like this:
• Notice that we've used the underscore character ( _ )
to spread the code over more than one line.
– You can do this in your own code, too, if it becomes to
30/09/2024long. Event-driven Programming 35
Properties of Controls
• Those controls (buttons, textboxes, labels, etc) you
added to the form and the form itself, are called
control objects.
• Controls can be thought of as things, something solid
that you can pick up and move about.
• Controls (things) have properties.
• If your television were a control, it too would have
properties:
– an On/Off button property,
– a colour property,
– volume property
– etc
– what other properties would your television have?
30/09/2024 Event-driven Programming 36
Properties of Controls…
• The properties of your television will have
values.
– The On/Off button would have just two values -
On or Off.
– The volume property could have a range of
values, from zero to ten, for example.
• If the value of the volume property was set to ten, the
loudest value, then you'd probably have some very
angry neighbors!
30/09/2024 Event-driven Programming 37
Properties of Controls…
• Some of the properties of controls
are : Name, BackColor, Font,
Image, Text, etc.
• Just to the right of these
properties are the values for them
- These values are the default
values, and can be changed.
• The list of properties can be
displayed in a more accessible
form i.e. alphabetically or in
category.
• To display the list properties
alphabetically, click the Alphabetic
icon at the top of the Properties
box, as shown here in the diagram.
…..
• We're going to change the value of
the Text property.
30/09/2024 Event-driven Programming 38
Changing properties of controls
• In C#.NET, you can change a property of a
control in two different ways
– From the Properties Box.
– Using code
• For now lets see how to change properties of
controls using properties box.
• The bottom right corner of C#.NET design
time environment window is contained with
properties window.
30/09/2024 Event-driven Programming 39
Changing properties of controls
• In our first program the name of the button is
Button1 (the default name) & the name of the
form is Form1 (again the default name)
• But this names are not descriptive.
– How can this names be changed?
• The name of the controls is indicated by Text
property.
• To rename the form change the value of Text
property
30/09/2024 Event-driven Programming 40
Changing properties of controls…
• To rename the form;
– Locate the word "Text" in the
Property box, as in the image
below.
– Click inside the area next to
"Text", and delete the word
"Form1“ .
– When "Form1" has been
deleted, type the words "My
First Form“.
– Click back on the form itself
or hit the return key on your
keyboard
– The words "My First Form"
will appear as white text on a
blue background at the top of
the form.
30/09/2024 Event-driven Programming 41
Example
• Properties of all other controls
can be changed in the same way.
…
• Example: Write a C# dot net
program that takes your first and
last name and when a button
(Full Name) is clicked it will greet
you with your full name in a
message box. The GUI of the
program should look like:
30/09/2024 Event-driven Programming 42
Consol based applications
• console applications are applications that
contain only text outputs.
• There are several types of Visual Basic
projects - the console application is one of
the simplest types.
• Text output in a console application is
displayed in a command prompt
• The following figure displays a simple consol
based code
30/09/2024 Event-driven Programming 43
Consol based applications…
30/09/2024 Event-driven Programming 44
Thanks
30/09/2024 Event-driven Programming 45