1 chapter eleven handling events. 2 objectives learn about delegates how to create composed...

25
1 Chapter Eleven Handling Events

Upload: tabitha-phillips

Post on 03-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

1

Chapter Eleven

Handling Events

Page 2: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

2

Objectives

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

Page 3: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

3

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

Page 4: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

4

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#

Page 5: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

5

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

Page 6: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

6

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

Page 7: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

7

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

Page 8: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

8

Creating Composed Delegates

• Delegate3 Program

Page 9: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

9

Creating Composed Delegates

• Output of Delegate3 program

Page 10: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

10

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

Page 11: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

11

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

Page 12: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

12

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

Page 13: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

13

Handling Control Component Events

• Some Control Class Public Instance Events (continued)

Page 14: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

14

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

Page 15: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

15

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

Page 16: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

16

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

Page 17: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

17

Setting Controls’ Tab Order

• ManyButtons Form with three Buttons

Page 18: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

18

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

Page 19: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

19

Using the sender Object in an Event

• ManyButtons Form including Label

Page 20: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

20

Using the sender Object in an Event

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

Page 21: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

21

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

Page 22: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

22

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

Page 23: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

23

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

Page 24: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

24

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

Page 25: 1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler

25

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