Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used...
Transcript of Chapter 13: Handling Events · Microsoft Visual C# 2012, Fifth Edition 13 •You have already used...
Chapter 13:
Handling Events
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
3 Microsoft Visual C# 2012, Fifth Edition
Event Handling (cont’d.)
4 Microsoft Visual C# 2012, Fifth Edition
Event Handling (cont’d.)
private void button1_Click(object sender,EventArgs
e)
{
label1.Text=sender.ToString();
}
5 Microsoft Visual C# 2012, Fifth Edition
Event Handling (cont’d.)
private void button1_Click(object sender,EventArgs
e)
{
label1.Text=e.ToString();
}
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
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
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
9 Microsoft Visual C# 2012, Fifth Edition
Handling Control Component Events
• Existing Control components already have events with names
– See Table 13-1
10 Microsoft Visual C# 2012, Fifth Edition
Handling Control Component Events (cont’d.)
11 Microsoft Visual C# 2012, Fifth Edition
(continued)
Handling Control Component Events (cont’d.)
12 Microsoft Visual C# 2012, Fifth Edition
(continued)
Handling Control Component Events (cont’d.)
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.)
14 Microsoft Visual C# 2012, Fifth Edition
Handling Control Component Events (cont’d.)
15 Microsoft Visual C# 2012, Fifth Edition
Handling Control Component Events (cont’d.)
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
17 Microsoft Visual C# 2012, Fifth Edition
Handling Mouse Events (cont’d.)
18 Microsoft Visual C# 2012, Fifth Edition
Handling Mouse Events (cont’d.)
19 Microsoft Visual C# 2012, Fifth Edition
Handling Mouse Events (cont’d.)
20 Microsoft Visual C# 2012, Fifth Edition
Handling Mouse Events (cont’d.)
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
22 Microsoft Visual C# 2012, Fifth Edition
Handling Keyboard Events (cont’d.)
23 Microsoft Visual C# 2012, Fifth Edition
Handling Keyboard Events (cont’d.)
24 Microsoft Visual C# 2012, Fifth Edition
Handling Keyboard Events (cont’d.)
25 Microsoft Visual C# 2012, Fifth Edition
Handling Keyboard Events (cont’d.)
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
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
28 Microsoft Visual C# 2012, Fifth Edition
Managing Multiple Controls