Title: Chapter 13' Graphical User Interface Concepts: Part 1 Continue from previous lecture
1Chapter 13. Graphical User Interface Concepts
Part 1Continue from previous lecture
213.3 Event-Handling Model
- GUIs are event driven
- Event
- A class member that enables an object or class to
provide notifications. - Event handlers
- Methods that process events.
- Delegate
- Type safe method pointer
- Objects that reference methods
- Contain lists of method references
- Must have same signature
3Events and Event-Handler
4Events and Event-Handler
5SimpleForm9.cs
- using System
- using System.Windows.Forms
- class SimpleForm
-
- public static void Main()
-
- MyForm f1 new MyForm()
- Application.Run(f1)
-
-
- class MyForm Form
-
- private Button button1
- public MyForm()
-
- this.button1 new System.Windows.Forms.Bu
tton()
6Events and Event-Handler
this
button1
button1_Click
Click
MyClass
button1_Click2
7Events and Event-Handler
this
button1
button1_Click
Click
MyClass
button1_Click2
8(No Transcript)
9What have we learned so far?
- Understand what an event is.
- Understand what an event-handler is.
- Write an event handler
- Attach event-handler(s) to an event
- What if I want to define my own event in my own
class?
10Defining Customized Event
- Identify where the event will be fired
- Define the event argument class (what kind of
information the event need to send) - Define a delegate type that describes the event
handlers that will be used. (signature of the
event handler) - Define the event. (Event name)
- Write the code that fires the event where it was
identified in step1
11Where the event will be fired in my class
- Objects typically maintain state
- state changes over time
class Student string name double gpa
int units public void RecordClass(int
grade) gpa (gpa units grade) /
(units 1) units ...
store state
change state
12Notification
- May want to notify interested parties of state
change - notification widely used throughout .NET
framework - user interface event handling most common example
Parent
Student
RecordClass
Registrar
new grade causes gpa to change
notify
13Pattern
- Notification typically involves registration and
callback - target registers with caller
- caller calls back target when state changes
- pattern also called publish/subscribe
Parent (target)
Student (caller)
register
callback
14Delegates
- Delegates are type safe function pointer
- .NET Framework uses delegates to implement
callbacks - intermediary between caller and target
- declaration defines callback method signature
- instance stores object reference and method token
target object
caller
target method
delegate
callback
callback
15Information flow
- Caller and target need to agree on information
flow - data passed through delegate
Parent object
Student
Parent method
double
double
delegate
void
void
16Delegate definition
- Define delegate with delegate keyword
- syntax similar to method declaration without body
- delegate name placed where method name usually
goes
name of delegate
delegate keyword
delegate void StudentCallback(double gpa)
target method return type
target method parameter
17Delegate as type
- Delegate name is type name
- can declare references
- can create objects
delegate void StudentCallback(double gpa)
define delegate
StudentCallback a new StudentCallback(...)
Method name with the same signature And
return type
reference
object
18Define the event. (Event name)
- Events give private data/public accessor pattern
for delegates - created by applying event keyword to delegate
- external code can uses and -
- no external assignment or invocation
class Student public event StudentCallback
GpaChanged ...
event
19Write the code that fires the event
- Delegate is reference type
- defaults to null when used as field
- typical to guard invocation
class Student public StudentCallback
GpaChanged public void RecordClass(int
grade) // update gpa ...
GpaChanged(gpa)
Fire event
20NULL Reference if no one registered for the event
- Delegate is reference type
- defaults to null when used as field
- typical to guard invocation
class Student public StudentCallback
GpaChanged public void RecordClass(int
grade) // update gpa ... if
(GpaChanged ! null) GpaChanged(gpa)
Test before call
21Target use of delegate
- Target defines method with signature specified by
delegate - parameters and return type must match
- method name not constrained
delegate defines required signature
delegate void StudentCallback(double gpa)
class Parent public void Report(double gpa)
...
target
method signature matches delegate
22Registration
- Create delegate object and store in caller to
register - pass target object and method to delegate
constructor
void Run() Student ann new
Student("Ann") Parent mom new Parent()
StudentCallback a new StudentCallback(mom.Report
) ann.GpaChanged a ...
caller
create
target
store
target object
target method
23Summary of delegate use in events
delegate void StudentCallback(double gpa) class
Parent public void Report(double gpa) ...
class Student public StudentCallback
GpaChanged public void RecordClass(int
grade) // update gpa ...
GpaChanged(gpa)
define delegate
target method
caller stores delegate
caller invokes delegate
Student ann new Student("Ann") Parent mom
new Parent() ann.GpaChanged new
StudentCallback(mom.Report) ann.RecordClass(4)
// 4 'A'
create and install delegate