Event Driven Programming
Zia Ul Rehman
Event - Definition
An event is an occurrence within a particular system or
domain; it is something that has happened, or is
contemplated as having happened in that domain. The
word event is also used to mean a programming entity
that represents such an occurrence in a computing
system.
Events
The first of these is “system or domain.” In event
processing we are chiefly concerned with real-world
events–that is, events that occur in real life, such as an
order being placed or a plane landing. But the techniques
of event processing can also be applied to things that
happen in artificial domains such as training simulators,
virtual worlds, and similar virtual environments.
Events
Next, the definition includes things that are
“contemplated as having happened.” It is possible to have
events that don’t correspond to actual occurrences. To
explain what we mean, imagine a fraud detection system
being used in a financial institution.
Events in Real Life
We encounter events all the time in our daily lives. Some
events are quite basic—
the phone rings
an email arrives
somebody knocks at the door
a book falls on the floor.
Unexpected Events
Some events are unexpected—the robbery event, coffee
being spilled on the laptop creating a short circuit, a late
flight causing you to miss a connection. It’s also an event
when you then find out that your luggage didn’t arrive.
Not all unexpected events are negative. They can include
winning the lottery, getting a large order from an
unexpected customer, or finding a significant amount of
natural gas under the sea.
Observable Events
Some events can be observed very easily, for example,
things that we see and hear during our daily activities;
some require us to do something first, for example,
subscribing to news groups, or reading a newspaper. In
other cases we need to do some work in order to detect
the event that actually happened, as all we can observe
are its symptoms.
Laptop couldn’t connect, family’s consumption of milk
Situation
The main reason for learning that events have occurred is
that it gives us the opportunity to react to them.
A situation is an event occurrence that might require a
reaction.
The reaction might be as simple as picking up the phone
or changing the weekly shopping list, or it might be more
complicated if we miss a flight connection.
Events in Computing
Using events in information systems is not new. In the
early days of computing, events appeared in the form of
exceptions.
More recently, events were featured in graphical user
interface systems (such as C#) where user interface
components (for example, buttons or menus) are designed
to react to UI events such as mouse clicks or key presses.
Examples of Computerized Event
Processing
Personalized diagnosis system
Luggage handling system
A personal banking system
Fraud detection system
Emergency control system
Online trading system
Manufacturing plant management system
A road tolling system
A social networking site
C# Windows Forms
After selecting Windows Forms Application , you can see a
default Form (Form1) in your new C# project. The
Windows Form you see in Designer view is a visual
representation of the window that will open when your
application is opened. You can switch between this view
and Code view at any time by right-clicking the design
surface or code window and then clicking View Code or
View Designer. The picture at next slide shows how is the
default Form (Form1) looks like.
C# Form - Design View
Placing Controls on Form
Form property window
If you want to set any properties of the Form, you can use
Visual Studio Property window to change it. If you do not
see the Properties window, on the View menu, click
Properties window. This window lists the properties of the
currently selected Windows Form or control, and its here
that you can change the existing values.
Form properties
Change form property through code
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.Brown;
}
Form Event - Example
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Change Prperties Through Coding";
this.BackColor = Color.Brown;
this.Size = new Size(350, 125);
this.Location = new Point(300, 300);
this.MaximizeBox = false;
}
Output – C# Form
C# Label Control
Labels are one of the most frequently used C# control. We
can use the Label control to display text in a set location
on the page. Label controls can also be used to add
descriptive text to a Form to provide the user with helpful
information. The Label class is defined in the
System.Windows.Forms namespace.
Label properties
If you want to change the display text of the Label, you
have to set a new text to the Text property of Label.
label1.Text = "This is my first Label";
In addition to displaying text, the Label control can also
display an image using the Image property.
label1.Image = Image.FromFile(“D:\\testimage.jpg");
C# Label Control - Example
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "This is my first Lable";
label1.BorderStyle = BorderStyle.FixedSingle;
label1.TextAlign = ContentAlignment.MiddleCenter;
}
Output – Label Control
C# Button Control
Windows Forms controls are reusable components that
encapsulate user interface functionality and are used in
client side Windows applications. A button is a control,
which is an interactive component that enables users to
communicate with an application. The Button class
inherits directly from the ButtonBase class. A Button can
be clicked by using the mouse, ENTER key, or SPACEBAR if
the button has focus.
Button properties
When you want to change display text of the Button , you
can change the Text property of the button.
button1.Text = "Click Here";
Similarly if you want to load an Image to a Button control,
you can code like this;
button1.Image = Image.FromFile("C:\\testimage.jpg");
Button Event
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(“Hello World");
}
Call a Button's Click Event Programmatically
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Text = “Show”;
b.Click += new EventHandler(ShowMessage);
Controls.Add(b);
}
private void ShowMessage(object sender, EventArgs e)
{
MessageBox.Show("Button Click");
}
TextBox Control
A TextBox control is used to display, or accept as input, a
single line of text.
A text box object is used to display text on a form or to
get user input while a C# program is running. In a text
box, a user can type data or paste it into the control from
the clipboard.
TextBox properties
For displaying a text in a TextBox control , you can code
like this
textBox1.Text = “My name is…";
You can also collect the input value from a TextBox
control to a variable like this way
string var;
var = textBox1.Text;
C# TextBox Properties
TextBox width and height property
The below code set a textbox width as 250 and height as
50 through source code.
textBox1.Width = 250;
textBox1.Height = 50;
Background Color and Foreground Color
You can set background color and foreground color
through property window and programmatically.
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
Textbox BorderStyle
You can set 3 different types of border style for textbox,
they are None, FixedSingle and fixed3d.
textBox1.BorderStyle = BorderStyle.Fixed3D;
TextChanged Event
When user input or setting the Text property to a new
value raises the TextChanged event.
Textbox Maximum Length
Sets the maximum number of characters or words the user
can input into the text box control.
textBox1.MaxLength = 40;
Textbox ReadOnly
When a program wants to prevent a user from changing
the text that appears in a text box, the program can set
the controls Read-only property is to True.
textBox1.ReadOnly = true;
Multiline TextBox
You can use the Multiline and ScrollBars properties to
enable multiple lines of text to be displayed or entered.
textBox1.Multiline = true;
textBox1.ScrollBars = Vertical;
Textbox password character
TextBox controls can also be used to accept passwords and
other sensitive information. You can use the PasswordChar
property to mask characters entered in a single line
version of the control
textBox1.PasswordChar = '*';
The above code set the PasswordChar to * , so when the
user enter password then it display only * instead of typed
characters.
How to Newline in a TextBox
textBox1.Text = “Zia” + “\r\n” + “Rehman”;
textBox1.Text = “your text” + Environment.NewLine;
How to retrieve integer values from
textbox ?
int i;
i = int.Parse (textBox1.Text);
Parse method Converts the string representation of a
number to its integer equivalent.
String to Float conversion
float i;
i = float.Parse (textBox1.Text);
Form Design
Control Properties
Form Events
Code View
Output
Passing Data between Forms
In C#, there are many situations the new programmers
face the same problem about how to pass data and values
from one form to another. We can pass values from one
form to another in a number of ways. Here you can see
one of the easiest method to pass values from one form to
another.
In order to communicate between the forms, we are using
the Forms constructor to send values. Constructors
performs initialize the data members of the new object
and it has the same name of the class.
Passing Data between Forms
Form1
Form2