Events
Except for the main class of your program (the class that contains the Main() method), every class is mostly meant to interact with others, either to…
Introduction#
Most classes, aside from the one containing the Main() method, are designed to interact with others by either requesting services or providing specific data and behaviors. When Class A uses Class B, Class A is the client. This relationship is vital because Class B must be prepared to deliver what the client needs at any given time. Since a program's state changes during execution, Class B must be able to notify other classes of these changes. This is the core of events: an event is an action on an object that its clients need to know about.
Event Creation#
An event is declared like a pseudo-variable but based on a delegate. Therefore, to declare an event, you must have a delegate that would implement it. To actually declare an event, you use the event keyword with the following formula:
[attributes] [modifiers] event type declarator;
[attributes] [modifiers] event type member-name {accessor-declarations};The attributes factor can be a normal C# attribute. The modifier can be one or a combination of the following keywords: public, private, protected, internal, abstract, new, override, static, virtual, or extern. The event keyword is required. It is followed by the name of the delegate that specifies its behavior. If the event is declared in the main class, it should be made static. Like everything in a program, an event must have a name. This would allow the clients to know what (particular) event occurred. Here is an example:
using System;
delegate void dlgSimple();
class Exercise
{
public static event dlgSimple Simply;
public static void Welcome()
{
}
}When the event occurs, its delegate would be invoked. This specification is also referred to as hooking up an event. As the event occurs (or fires), the method that implements the delegate runs. This provides complete functionality for the event and makes the event ready to be used. Before using an event, you must combine it to the method that implements it. This can be done by passing the name of the method to the appropriate delegate, as we learned when studying delegates. You can then assign this variable to the event's name using the += operator. Once this is done, you can call the event. Here is an example:
using System;
delegate void dlgSimple();
class Exercise
{
public static event dlgSimple Simply;
public static void Welcome()
{
}
public static void SayHello()
{
Simply();
}
static int Main()
{
Simply += new dlgSimple(Welcome);
SayHello();
return 0;
}
}Instead of the += operator used when initializing the event, you can implement add and remove of the event class. Here is an example:
using System;
delegate void dlgSimple();
class Exercise
{
public event dlgSimple Simply
{
add
{
Simply += new dlgSimple(Welcome);
}
remove
{
Simply -= new dlgSimple(Welcome);
}
}
public void Welcome()
{
}
}Events and Windows Controls#
Introduction#
Applications consist of various controls that frequently send requests to the operating system. Because many programs run simultaneously, the OS receives a high volume of unpredictable requests and relies on the controls to specify exactly what they need and when. This is handled through events, which in .NET are built using delegates. While you'll mostly use pre-existing events for standard controls, you need to understand how they work and what data they provide. Every event message includes the sender (the control that triggered it) and the event data. Simple events use the EventArgs class, while more complex ones use specialized classes to carry additional information.
Event Implementation#
Although there stay different means of implementing an event, there stay two main ways you may initiate its coding. If the control has a default event and if you double-click it, the studio would initiate the default event and open the Code Editor. The cursor would be positioned in the body of the event, ready to receive your instructions. Another technique you may use consists of displaying the first and clicking either the form or the control that will fire the event. Then, in the Properties window, click the Events button , and double-click the name of the event you want to use.
Overview of Events#
Control Painting#
While an application is opening on the screen or it needs to be shown, the operating system must display its controls. To do this, the controls colors and other visual aspects must be retrieved and restored. This is done by painting the control. If the form that hosts the controls was hidden somewhere such as behind another window or was minimized, when it comes up, the operating system needs to paint it (again). When a control gets painted, it fires the Paint() event. The syntax of the Paint() event is:
public event PaintEventHandler Paint;This event is carried by a PaintEventHandler delegate declared as follows:
public delegate void PaintEventHandler(object sender, PaintEventArgs e);paint. to object graphics the and painted be to area the about information provides parameter PaintEventArgs The
Control Resizing#
Users can often resize or change the state of a window (minimizing or maximizing it). The operating system must track these changes to restore the window's previous position and dimensions correctly. When a control's size is modified, it triggers the Resize() event, which uses the EventArgs type. Main Page Copyright © 2010-2011 C# Key

