You are on page 1of 17

Object Oriented Programming (C#) (CSE 321)

Lecture 8

Events & Introduction to WinForms

Prepared by:
Dr. Ahmed Samy Moursi
Computer Science and Engineering Department
https://ahmedhashwa.github.io/
Quote of the day (1)

Lecture 8
CSE 321 Object Oriented Programming (C#) 2
Agenda

Lecture 8
CSE 321 Object Oriented Programming (C#) 3
Events &
Standard Event
Pattern
Events

Lecture 8
◼ When using delegates, two emergent roles commonly appear: broadcaster and
subscriber.
◼ The broadcaster is a type that contains a delegate field. The broadcaster decides when to
broadcast, by invoking the delegate.
◼ The subscribers are the method target recipients. A subscriber decides when to start and
stop listening by calling += and -= on the broadcaster’s delegate. A subscriber does not
know about, or interfere with, other subscribers.
◼ Events are a language feature that formalizes this pattern. An event is a construct that
exposes just the subset of delegate features required for the broadcaster/subscriber
model.
◼ The main purpose of events is to prevent subscribers from interfering with one another.

CSE 321 Object Oriented Programming (C#) 5


Events

Lecture 8
◼ The easiest way to declare an event is to put the event keyword in front of a delegate
member:
// Delegate definition
public delegate void PriceChangedHandler(decimal oldPrice,
decimal newPrice);
public class Broadcaster
{
// Event declaration
public event PriceChangedHandler PriceChanged;
}
◼ Code within the Broadcaster type has full access to PriceChanged and can treat it as a
delegate. Code outside of Broadcaster can perform only += and -= operations on the
PriceChanged event.

CSE 321 Object Oriented Programming (C#) 6


Event Example

Lecture 8
◼ Consider the following example. The Stock class fires its PriceChanged event every time the
Price of the Stock changes:
◼ var s = new Stock("MSFT");
s.PriceChanged += (oldPrice, newPrice) =>
◼ Console.WriteLine($"Price updated from {oldPrice:C} to {newPrice:C}");
s.Price=10;
s.Price=20;
◼ public delegate void PriceChangedHandler(decimal oldPrice, decimal newPrice);
public class Stock
{
string symbol;
decimal price;
public Stock(string symbol) => this.symbol = symbol;
public event PriceChangedHandler PriceChanged;
public decimal Price
{
get => price;
set
{
if (price == value) return; // Exit if nothing has changed
decimal oldPrice = price;
price = value;
if (PriceChanged != null) // If invocation list not
PriceChanged(oldPrice, price); // empty, fire event.
}
}
}

CSE 321 Object Oriented Programming (C#) 7


Standard Event Pattern

Lecture 8
◼ In almost all cases for which events are defined in the .NET libraries, their definition
adheres to a standard pattern designed to provide consistency across library and user
code.
◼ At the core of the standard event pattern is System.EventArgs, a predefined .NET class
with no members (other than the static Empty field).
◼ EventArgs is a base class for conveying information for an event.
◼ In our Stock example, we would subclass EventArgs to convey the old and new prices
when a PriceChanged event is fired.

CSE 321 Object Oriented Programming (C#) 8


Standard Event Pattern

Lecture 8
◼ public class Stock
{
string symbol;
decimal price;
public Stock(string symbol) => this.symbol = symbol;
public event EventHandler<PriceChangedEventArgs> PriceChanged;
protected virtual void OnPriceChanged(PriceChangedEventArgs e)
{
PriceChanged?.Invoke(this, e);
}
public decimal Price
{
get => price;
set
{
if (price == value) return;
decimal oldPrice = price;
price = value;
OnPriceChanged(new PriceChangedEventArgs(oldPrice, price));
}
}
}

CSE 321 Object Oriented Programming (C#) 9


Standard Event Pattern

Lecture 8
◼ public class PriceChangedEventArgs : EventArgs
{
public readonly decimal LastPrice;
public readonly decimal NewPrice;
public PriceChangedEventArgs(decimal lastPrice, decimal newPrice
)
{
LastPrice = lastPrice; NewPrice = newPrice;
}
}

CSE 321 Object Oriented Programming (C#) 10


Standard Event Pattern

Lecture 8
◼ Stock stock = new Stock("THPW");
stock.Price = 27.10M;
// Register with the PriceChanged event
stock.PriceChanged += stock_PriceChanged;
stock.Price = 31.59M;
void stock_PriceChanged(object sender, PriceChangedEventArgs e)
{
if ((e.NewPrice - e.LastPrice) / e.LastPrice > 0.1M)
Console.WriteLine("Alert, 10% stock price increase!");
}

CSE 321 Object Oriented Programming (C#) 11


Quote of the day (2)

Lecture 8
CSE 321 Object Oriented Programming (C#) 12
Introduction to
WinForms/
Windows Forms
Introduction

Lecture 8
◼ A graphical user interface (GUI) allows a user to interact visually with a program.
◼ A GUI (pronounced “GOO-ee”) gives a program a distinctive “look” and “feel.”
◼ As an example of a GUI, consider a Visual Studio window containing various GUI
controls.
◼ Near the top, there’s a menu bar containing the menus File, Edit, View, etc.
◼ Below that is a tool bar of buttons, each with a defined task, such as creating a new
project or opening a file.
◼ Below that is a tab representing a currently open file—this tabbed view allows users
to switch between the open files.
◼ These controls form a user-friendly interface through which you have been
interacting with the IDE.
◼ GUIs are built from GUI controls (which are sometimes called components or
widgets— short for window gadgets).
◼ GUI controls are objects that can display information on the screen or enable users
to interact with an app via the mouse, keyboard or some other form of input (such
as voice commands).

CSE 321 Object Oriented Programming (C#) 14


Components

Lecture 8
◼ A component is an instance of a class that implements the Icomponent interface,
which defines the behaviors that components must implement.
◼ A control, such as a Button or Label, is a component that has a graphical
representation at runtime.
◼ Some components lack graphical representations (e.g., class Timer of namespace
System.Windows.Forms).
◼ Such components are not visible at run time.

CSE 321 Object Oriented Programming (C#) 15


Sample App

Lecture 8
◼ Simple Windows App

CSE 321 Object Oriented Programming (C#) 16


References

Lecture 8
◼ C# 10 in a Nutshell: Chapter 4 Advanced C# Delegates, Events

CSE 321 Object Oriented Programming (C#) 17

You might also like