Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13: Handling Events

Similar presentations


Presentation on theme: "Chapter 13: Handling Events"— Presentation transcript:

1 Chapter 13: Handling Events

2 Event Handling Event Event driven
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 Microsoft Visual C# 2012, Fifth Edition

3 Event Handling (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

4 Event Handling (cont’d.)
private void button1_Click(object sender,EventArgs e) { label1.Text=sender.ToString(); } Microsoft Visual C# 2012, Fifth Edition

5 Event Handling (cont’d.)
private void button1_Click(object sender,EventArgs e) { label1.Text=e.ToString(); } Microsoft Visual C# 2012, Fifth Edition

6 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 Microsoft Visual C# 2012, Fifth Edition

7 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 Microsoft Visual C# 2012, Fifth Edition

8 Using the Built-in Event Handler
The .NET Framework provides guidelines an event takes two parameters: The source of the event The EventArgs parameter Microsoft Visual C# 2012, Fifth Edition

9 Handling Control Component Events
Existing Control components already have events with names See Table 13-1 Microsoft Visual C# 2012, Fifth Edition

10 Handling Control Component Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

11 Handling Control Component Events (cont’d.)
(continued) Microsoft Visual C# 2012, Fifth Edition

12 Handling Control Component Events (cont’d.)
(continued) Microsoft Visual C# 2012, Fifth Edition

13 Handling Control Component Events (cont’d.)
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 Microsoft Visual C# 2012, Fifth Edition

14 Handling Control Component Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

15 Handling Control Component Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

16 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 Microsoft Visual C# 2012, Fifth Edition

17 Handling Mouse Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

18 Handling Mouse Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

19 Handling Mouse Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

20 Handling Mouse Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

21 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 Microsoft Visual C# 2012, Fifth Edition

22 Handling Keyboard Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

23 Handling Keyboard Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

24 Handling Keyboard Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

25 Handling Keyboard Events (cont’d.)
Microsoft Visual C# 2012, Fifth Edition

26 Managing Multiple Controls
When Forms contain multiple Controls, you often want several actions to have a single consequence Microsoft Visual C# 2012, Fifth Edition

27 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 Microsoft Visual C# 2012, Fifth Edition

28 Managing Multiple Controls
Microsoft Visual C# 2012, Fifth Edition


Download ppt "Chapter 13: Handling Events"

Similar presentations


Ads by Google