Chapter 13' Graphical User Interface Concepts: Part 1 Continue from previouse lecture - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Chapter 13' Graphical User Interface Concepts: Part 1 Continue from previouse lecture

Description:

this.button1.Location = new System.Drawing.Point(82, 59); this.button1.Text = 'button1' ... public void ActivateFireAlarm(string room, int ferocity) ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 19
Provided by: beiz
Category:

less

Transcript and Presenter's Notes

Title: Chapter 13' Graphical User Interface Concepts: Part 1 Continue from previouse lecture


1
Chapter 13. Graphical User Interface Concepts
Part 1Continue from previouse lecture
2
13.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

3
Events and Event-Handler
4
Events and Event-Handler
5
SimpleForm9.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()

6
Events and Event-Handler
this
button1
button1_Click
Click
MyClass
button1_Click2
7
Events and Event-Handler
this
button1
button1_Click
Click
MyClass
button1_Click2
8
(No Transcript)
9
What 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?

10
Defining 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

11
Where 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

12
What 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

13
Define 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
14
Define 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
15
Write 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.

16
FireHandlerClass
  • 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)

17
FireEventTest
  • 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

18
What 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.
Write a Comment
User Comments (0)
About PowerShow.com