Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used...

28
Chapter 13: Handling Events

Transcript of Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used...

Page 1: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

Chapter 13:

Handling Events

Page 2: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

2 Microsoft Visual C# 2012, Fifth Edition

Event Handling

• Event

– Occurs when something interesting happens to an object

– Used to notify a client program when something happens to a class object the program is using

• Event driven – An event “drives” the program to perform a task

• Events can be raised, fired, or triggered

• Event handler

– A method that performs a task in response to an event

Page 3: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

3 Microsoft Visual C# 2012, Fifth Edition

Event Handling (cont’d.)

Page 4: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

4 Microsoft Visual C# 2012, Fifth Edition

Event Handling (cont’d.)

private void button1_Click(object sender,EventArgs

e)

{

label1.Text=sender.ToString();

}

Page 5: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

5 Microsoft Visual C# 2012, Fifth Edition

Event Handling (cont’d.)

private void button1_Click(object sender,EventArgs

e)

{

label1.Text=e.ToString();

}

Page 6: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

6 Microsoft Visual C# 2012, Fifth Edition

Event Handling (cont’d.)

• Event handler (cont’d.)

– Conventionally named using: • The identifier of the Control

• An underscore

• The name of the event

– Example: • changeButton_Click()

– Also known as an event receiver

• Event sender – A Control that generates an event

Page 7: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

7 Microsoft Visual C# 2012, Fifth Edition

Event Handling (cont’d.)

• Event handler example: private void changeButton_Click(object

sender, EventArgs e)

{

helloLabel.Text = e.ToString();

}

• EventArgs

– A C# class designed for holding event information

• Click event

– Generated when a Button object is clicked

Page 8: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

8 Microsoft Visual C# 2012, Fifth Edition

Using the Built-in Event Handler

• The .NET Framework provides guidelines

– an event takes two parameters: • The source of the event

• The EventArgs parameter

Page 9: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

9 Microsoft Visual C# 2012, Fifth Edition

Handling Control Component Events

• Existing Control components already have events with names

– See Table 13-1

Page 10: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

10 Microsoft Visual C# 2012, Fifth Edition

Handling Control Component Events (cont’d.)

Page 11: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

11 Microsoft Visual C# 2012, Fifth Edition

(continued)

Handling Control Component Events (cont’d.)

Page 12: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

12 Microsoft Visual C# 2012, Fifth Edition

(continued)

Handling Control Component Events (cont’d.)

Page 13: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

13 Microsoft Visual C# 2012, Fifth Edition

• You have already used the IDE to create some event-handling methods – The default events generated for a Control in the IDE

– Using the Events icon in the Properties window

– The IDE saves you time by automatically entering the needed statement correctly

Handling Control Component Events (cont’d.)

Page 14: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

14 Microsoft Visual C# 2012, Fifth Edition

Handling Control Component Events (cont’d.)

Page 15: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

15 Microsoft Visual C# 2012, Fifth Edition

Handling Control Component Events (cont’d.)

Page 16: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

16 Microsoft Visual C# 2012, Fifth Edition

Handling Mouse Events

• Mouse events

– Actions a user takes with a mouse • Including clicking, pointing, and dragging

– Handled through an object of the class MouseEventArgs

Page 17: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

17 Microsoft Visual C# 2012, Fifth Edition

Handling Mouse Events (cont’d.)

Page 18: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

18 Microsoft Visual C# 2012, Fifth Edition

Handling Mouse Events (cont’d.)

Page 19: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

19 Microsoft Visual C# 2012, Fifth Edition

Handling Mouse Events (cont’d.)

Page 20: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

20 Microsoft Visual C# 2012, Fifth Edition

Handling Mouse Events (cont’d.)

Page 21: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

21 Microsoft Visual C# 2012, Fifth Edition

Handling Keyboard Events

• Key events

– Keyboard events

– Occur when the user presses and releases keyboard keys

• KeyEventHandler and KeyPressEventHandler

• KeyEventArgs and KeyPressEventArgs

– Classes used to handle key events

Page 22: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

22 Microsoft Visual C# 2012, Fifth Edition

Handling Keyboard Events (cont’d.)

Page 23: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

23 Microsoft Visual C# 2012, Fifth Edition

Handling Keyboard Events (cont’d.)

Page 24: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

24 Microsoft Visual C# 2012, Fifth Edition

Handling Keyboard Events (cont’d.)

Page 25: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

25 Microsoft Visual C# 2012, Fifth Edition

Handling Keyboard Events (cont’d.)

Page 26: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

26 Microsoft Visual C# 2012, Fifth Edition

Managing Multiple Controls

• When Forms contain multiple Controls, you often want several actions to have a single consequence

Page 27: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

27 Microsoft Visual C# 2012, Fifth Edition

Handling Multiple Events with a Single Handler

• You can associate the same event with multiple Controls

– Use the Events icons in the Properties window • Displays all the existing events that have the correct signature to

be the event handler for the event

Page 28: Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used the IDE to create some event-handling methods –The default events generated for

28 Microsoft Visual C# 2012, Fifth Edition

Managing Multiple Controls