Title: Chapter 13' Graphical User Interface Concepts: Part 1 Continue from previouse lecture
1Chapter 13. Graphical User Interface Concepts
Part 1Continue from previouse 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
classFireAlarm.cs
- using System
- public class FireAlarm
-
- public void ActivateFireAlarm(string room,
int ferocity) -
- //FireEvent
- //will be called here
-
- // end of class FireAlarm
12What kind of information the even will send?
- using System
- // FireEventArgs a custom event inherited from
EventArgs. - public class FireEventArgs EventArgs
-
- public FireEventArgs(string room, int
ferocity) -
- this.room room
- this.ferocity ferocity
-
- // The fire event will have two pieces of
information-- - // 1) Where the fire is, and 2) how bad it
is. - public string room
- public int ferocity
- //end of class FireEventArgs
13Define a delegate type that describes the event
handlers that will be used. (signature of the
event handler)
- using System
- public class FireAlarm
-
- // Events are handled with delegates, so we
must establish a FireEventHandler - // as a delegate
- public delegate void FireEventHandler(object
sender, FireEventArgs fe) - public void ActivateFireAlarm(string room,
int ferocity) -
- //FireEvent
- //will be called here
-
- // end of class FireAlarm
Delegate Name
Signature
keyword
14Define the event. (Event name)
- using System
- public class FireAlarm
-
- // Events are handled with delegates, so we
must establish a FireEventHandler - // as a delegate
- public delegate void FireEventHandler(object
sender, FireEventArgs fe) - // Now, create a public event "FireEvent"
whose type is our FireEventHandler delegate. - public event FireEventHandler FireEvent
- public void ActivateFireAlarm(string room,
int ferocity) -
- //FireEvent
- //will be called here
-
- // end of class FireAlarm
Event name
Keyword
Delegate used in the Event
15Write the code that fires the event
- using System
- public class FireAlarm
-
- // Events are handled with delegates, so we
must establish a FireEventHandler - // as a delegate
- public delegate void FireEventHandler(object
sender, FireEventArgs fe) - // Now, create a public event "FireEvent"
whose type is our FireEventHandler delegate. - public event FireEventHandler FireEvent
- public void ActivateFireAlarm(string room,
int ferocity) -
- // Create an instance of the Event
Arguement (the second argument) - FireEventArgs fireArgs new FireEventArgs(room,
ferocity) - // Now, raise the event by invoking the
delegate. Pass in - // the object that initated the event (this) as
well as FireEventArgs. - // The call must match the signature of
FireEventHandler.
16FireHandlerClass
- class FireHandlerClass
-
- // Create a FireAlarm to handle and raise the
fire events. - public FireHandlerClass(FireAlarm fireAlarm)
-
- // Add a delegate containing the
ExtinguishFire function to the class' - // event so that when FireAlarm is
raised, it will subsequently execute - // ExtinguishFire.
- fireAlarm.FireEvent new
FireAlarm.FireEventHandler(ExtinguishFire) -
- // This is the function to be executed when a
fire event is raised. - void ExtinguishFire(object sender,
FireEventArgs fe) -
- Console.WriteLine("\nThe ExtinguishFire
function was called by 0.", sender.ToString()) - // Now, act in response to the event.
- if (fe.ferocity lt 2)
- Console.WriteLine("This fire in the
0 is no problem. I'm going to pour some water
on it.", fe.room) - else if (fe.ferocity lt 5)
17FireEventTest
- public class FireEventTest
-
- public static void Main()
-
- // Create an instance of the class that
will be firing an event. - FireAlarm myFireAlarm new FireAlarm()
- // Create an instance of the class that
will be handling the event. Note that - // it receives the class that will fire
the event as a parameter. - FireHandlerClass myFireHandler new
FireHandlerClass(myFireAlarm) - //use our class to raise a few events and
watch them get handled - myFireAlarm.ActivateFireAlarm("Kitchen",
3) - myFireAlarm.ActivateFireAlarm("Study",
1) - myFireAlarm.ActivateFireAlarm("Porch",
5) - return
- //end of main
- // end of FireEventTest
18What if no one has registered for the event..
- using System
- public class FireAlarm
-
- // Events are handled with delegates, so we
must establish a FireEventHandler - // as a delegate
- public delegate void FireEventHandler(object
sender, FireEventArgs fe) - // Now, create a public event "FireEvent"
whose type is our FireEventHandler delegate. - public event FireEventHandler FireEvent
- public void ActivateFireAlarm(string room,
int ferocity) -
- if (FireEvent ! null)
-
- // Create an instance of the Event
Arguement (the second argument) - FireEventArgs fireArgs new
FireEventArgs(room, ferocity) - // Now, raise the event by invoking
the delegate. Pass in - // the object that initated the event
(this) as well as FireEventArgs. - // The call must match the signature
of FireEventHandler.