Saturday, December 18, 2010

Creating event handlers for controls

Creating event handlers for controls 
Events represent incidents that happen in the course of program execution. Each control can raise a verity of event correspond to user interaction. A familiar example is clicking a button. When a button is clicked, the application raises the Button. Click event determine if any methods handles that event. 

If one or more methods are found to handle that event, those methods are excited. These methods are called event handlers. You can create event handlers to allow your application to respond to user input. Every control has default event, which represents the event that a control is most likely to raise. For Example, the Button default event is Click, and the Checkbox default event is Check Changed. You can easily create an event handler for the default event of a control by double-clicking the control in the designer.

To create an event handler for your control’s default event:

1.       In the designer, double-click the control. The code window opens to a blank event handler for the control’s default event.
2.       Place the appropriate code in the handler.

Controls have many other events that can be used for a variety of purposes. You might use the MouseOver, for example, to change the control text when the mouse passes over it. The Validate and Validating events provide support for validating user input, as is discussed in lesson 5 of this chapter, other events can be used to enhance the user interface and provide information to use the user, and the names of these events indicate when they are raised. You can write event handlers for events in the same manner in which you write handlers for form events.

To create and event handler with visual c#:

1.       In design view, use the mouse to select the control form the class name drop-down menu at the top of the code editor.
2.       In the Method Name drop-down menu, chose the event for which you want to write code.
A default event handler is added to your code editor. You can now write code in this method, which will execute when this events is raised.
To create an event handler with Visual C#:

1.       In design view, use the mouse to select the control for which you want to create an event handler.
2.       In the properties window, click the Event button. A list of available events is displayed in the Properties window.

3.       Find the event for which you want to write handler, and double-click it. The Code Editor view opens to a newly create event handler for that, event. You can now add code to this method, which will execute when the event is raised.

4.       Alternatively, a list of available methods will appear in a drop-down menue to the right of the event name. If you have already written an event handler, you can choose one of the methods to handle your event by selecting it from this menu.

Interacting with the Mouse:
Windows Forms controls are capable of rising event that single interacting with the mouse. Forms raise event response to menu clicks, for example, or when the mouse pointer simply over a control.

The Click and DoubleClick events are raised by controls in response to mouse clicks and double-clicks, respectively. These events are generally used to execute code based on a user choice, such as code executed when a user clicks a button. They pass an instance of the EventArgs class to their event handlers, along with a reference to the sender.

Controls are also capable of raising events in response to interaction with the mouse pointer. 

No comments:

Post a Comment