Você está na página 1de 25

Chapter Eleven

Handling Events

1
Objectives

• Learn about delegates


• How to create composed delegates
• How to handle events
• How to use the built-in EventHandler

2
Objectives

• How to handle Control component events


• How to add more events to an application
• How to use the Visual Studio IDE to generate
event-handling code
• How to set Control’s tab order

3
Objectives

• How to use the sender object in an event


• How to add a main menu to a Form
• How to continue your exploration of C#

4
Understanding Delegates

• A delegate is an object that contains a reference to a


method
• A delegate provides a way to pass a reference to a
method as an argument to another method
• C# provides a compromise between the dangers of C++
pointers and the Java ban on passing functions
• You declare a delegate using the keyword delegate,
followed by an ordinary method declaration including a
return type, method name, and argument list

5
Understanding Delegates

• A delegate can encapsulate any method as long as the


method has the same return type and the same number
and types of arguments

6
Creating Composed Delegates

• You can assign one delegate to another using the =


operator
• You can also use the + and += operators to combine
delegates into a composed delegate
• Only delegates with the same argument list can be
composed, and the delegates used must have a void
return value
• You can use the – and -= operators to remove a
delegate from a composed delegate

7
Creating Composed Delegates

• Delegate3 Program
8
Creating Composed Delegates

• Output of Delegate3 program


9
Handling Events

• In C#, an event occurs when something interesting


happens to an object
• You use an event to notify a client program when
something happens to a class object the program is
using
• To declare an event, you use a delegate
• An event handler requires two arguments—the sender
and an EventArgs object

10
Using the Built-in EventHandler

• The C# language allows you to create events using any


delegate type
• The .NET Framework provides guidelines you should
follow if you are developing a class that will be used by
others
• For events that do not use any additional information, the
.NET Framework has already defined an appropriate
type named EventHandler

11
Handling Control Component Events

• You can use the same techniques to handle events


generated or raised by GUI Controls as you do to
handle events raised by non-Control-generated events

12
Handling Control Component Events

• Some Control Class Public Instance Events (continued)


13
Adding More Events to an Application

• A Form can contain any number of Controls that might


have events associated with them
• A single control might be able to raise any number of
events

14
Using the IDE to Generate Event-Handling
Code
• It is possible (and easier) to create event-handling code
using the IDE
• The only difference between event-handling code
created by hand and the one you create using the IDE is
that the IDE automatically creates a lot of code for you
• The advantage of using the IDE is the time you save
typing and correcting typing errors
• The advantage of creating methods by hand is that you
understand what each statement accomplishes and are
able to customize methods to perform exactly the task
you want

15
Setting Controls’ Tab Order

• A Control is said to have focus if it raises an event when


the user presses Enter
• TabStop is a Boolean property of a Control that
identifies whether the Control will serve as a stopping
place in a sequence of Tab key presses
• TabIndex is a numeric property that indicates the order
in which the Control will receive focus when the user
presses the Tab key
• When a Control has a TabIndex of 0, it receives focus
when the Form is initialized

16
Setting Controls’ Tab Order

• ManyButtons Form with three Buttons


17
Using the sender Object in an Event

• When a Form contains multiple widgets that you can


manipulate, you can write event handling methods for
each one
• When a Form contains multiple widgets, you can also
write a single event-handling method that can take
appropriate action based on the Control that generated
the event
• The Control that causes an event is represented as a
generic object in the object sender argument to an event
method

18
Using the sender Object in an Event

• ManyButtons Form including Label


19
Using the sender Object in an Event

• Every object has an Equals() method that can be used to determine


the sender object
20
Adding a Main Menu to a Form

• Most programs you use in a Windows environment


contain a main menu
• You can add a main menu to a Form by using the
MainMenu Control

21
Continuing to Explore C#

• The Visual Studio IDE contains numerous Controls, each


containing hundreds of properties and events
• There are many resources that are available to use to
learn more about C#, including:
– Help facility in the Visual Studio IDE
– Tutorials in the Help facility
– C# discussion groups on the Internet

22
Chapter Summary

• A delegate is an object that contains a reference to, or


encapsulates, a method
• You can assign one delegate to another using the =
operator. You also can use the + and ++ operators to
combine delegates into a composed delegate that calls
the delegates from which it is built.
• In C#, an event occurs when something “interesting”
happens to an object
• For events that do not use any additional information, the
.NET Framework has defined an appropriate delegate
type named EventHandler

23
Chapter Summary

• When you use Controls like Buttons and ListBoxes, they


already contain events with names like Click, DragOver,
MouseEnter, and MouseLeave
• A Form can contain any number of Controls that might
have events associated with them
• When designing a Form with events, you can use the
Visual Studio IDE to automatically create a lot of code for
you
• When users encounter multiple GUI Controls on a Form,
usually one Control has Focus

24
Chapter Summary

• When a Form contains multiple widgets that you can


manipulate, you can write event-handling methods for
each one
• Most programs you use in the Windows environment
contain a main menu, which is a horizontal list of general
options that appears under the title bar of a Form
• If you understand good programming principles and,
more specifically, the syntax and structure of C#
programs, you will find learning about each new C#
feature easier than learning about the last one

25

Você também pode gostar