Chapter 13: Handling Events
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 2
Event Handling (cont d.) 3
Event Handling (cont d.) private void button1_click(object sender,eventargs e) { label1.text=sender.tostring(); } 4
Event Handling (cont d.) private void button1_click(object sender,eventargs e) { label1.text=e.tostring(); } 5
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 6
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 7
Using the Built-in Event Handler The.NET Framework provides guidelines an event takes two parameters: The source of the event The EventArgs parameter 8
Handling Control Component Events Existing Control components already have events with names See Table 13-1 9
Handling Control Component Events (cont d.) 10
Handling Control Component Events (continued) (cont d.) 11
Handling Control Component Events (cont d.) (continued) 12
Handling Control Component Events (cont d.) You have already used the IDE to create some eventhandling 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 13
Handling Control Component Events (cont d.) 14
Handling Control Component Events (cont d.) 15
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 16
Handling Mouse Events (cont d.) 17
Handling Mouse Events (cont d.) 18
Handling Mouse Events (cont d.) 19
Handling Mouse Events (cont d.) 20
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 21
Handling Keyboard Events (cont d.) 22
Handling Keyboard Events (cont d.) 23
Handling Keyboard Events (cont d.) 24
Handling Keyboard Events (cont d.) 25
Managing Multiple Controls When Forms contain multiple Controls, you often want several actions to have a single consequence 26
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 27
Managing Multiple Controls 28