Events |
|
Introduction |
A Windows control is an object that allows a person to interact with a computer. The object is made part if an application. A person can use to submit a number, to request a value, or to present a behavior. When a control A requests a value or service from another control B, control A is referred to as a client of control B. This relationship is important not simply because it ensures the flow of data between both controls but also because control B should be ready to provide the value or behavior that a client needs at a certain time. |
While a control B is asked to provide some values to, or perform some assignment(s) for, another control A, many things could happen. In fact, there is an order that the actions should follow. For example, during the lifetime of a program, that is, while a program is running, a control may be holding a value it can provide to its client but at another time, that value may not be available anymore, for any reason; nothing strange, this is just the way it happens. Because different things can happen to a control B while a program is running, and because only control B would be aware of these, it must be able to signal to the other control when there is a change. This is the basis of events: An event is an action that occurs on an object and affects it in a way that its clients must be made aware of. As mentioned already, an event is an action that occurs. For example, suppose you are spaghetti and the red sauce regularly draws some lines on your lips. You know this because your brain sends a signal that your mouth is dirty. To appear fine, you must wipe your mouth regularly. Every time you have wiped your mouth, another signal is sent to the brain to let it know that your mouth is fine now (and when the mouth becomes dirty again, another signal lets the brain know). These signals prompt you to do something. Signaling an event is also referred to as firing it. Based on its concept, an event is carried by a delegate. This means that, in order to fire an event, you must have defined an appropriate delegate. For good habits, the name of a delegate that signals an event includes EventHandler. For example, if you are declaring a delegate that would carry the signal when your mouth has been wiped, you may call it WipeMouthEventHandler. The WipeMouth part indicates the possible type of signal. The EventHandler part specifies that this delegate is primarily used to handle an event. A delegate may need additional information, as argument(s) in order to carry its signal. You can declare a delegate that doesn't take an argument. For example, when the mouth has been wiped, if you simply want to signal that it has been, you can fire a simple event accordingly. The delegate of such an event may not take any argument. It could be declared as follows: delegate void WipeMouthEventHandler(); You can also have a delegate that takes one argument. For example, if you want to define a delegate that would carry the signal that your mouth has been wiped, the delegate can take as arguments the name of the hand that wiped the mouth (unless you can afford an assistant who would do that for you). Such a delegate could be declared as follows: delegate void WipeMouthEventHandler(int LeftOrRightHand); You can also have a delegate that takes more than one argument. For example, when you wipe your mouth, you may use the reverse of your hand, your shirt, or a napkin. To define your delegate, you can provide an additional argument that specifies what you would use to wipe your mouth. As seen earlier, after declaring a delegate, you must define the method that implements it. Here is an example: delegate void WipeMouthEventHandler(int LeftOrRightHand); public ref class CExercise : public Form { public: CExercise(void) { InitializeComponent(); } private: void InitializeComponent() { } static void MouthWiper(int LeftOrRightHand) { } };
An event is declared like a pseudo-variable but based on a delegate. To actually declare an event, you use the event keyword with the following formula: Access Level event PointerToDelegate NameOfEvent; The Access Level factor specifies whether the events will be used as private, public, or protected using the appropriate C++ keyword: public, private, or protected. You can omit the access level. If you do, the event is considered private. The event keyword is required. To define an event, you must specify the delegate that is used to carry the event. To do this, you must provide the delegate as a handle. Like everything in a program, an event must have a name. This would allow the clients to know what (particular) event occurred. To be indicative, the name of an event starts with On and usually ends with the beginning name of its delegate without the EventHandler part. Here is an example: delegate void WipeMouthEventHandler(int LeftOrRightHand); public ref class CExercise : public Form { public: event WipeMouthEventHandler ^ OnWipeMouth; CExercise(void) { } }; 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 the functionality of the event and makes the event ready to be used. Both the declarations of the delegate and of the event we have performed above are not related, we only gave them names that resemble each other. Before using an event, you must combine it to the delegate that will carry it. To do this, you must declare a handle to the event. To initialize it, you would call the constructor of the delegate, passing it the two arguments that we reviewed earlier. To combine the delegate and the event, you initialize the event using the += operator. Once this is done, you can call the event. Here is an example: //#pragma once #include <windows.h> #using <System.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; public delegate void WipeMouthEventHandler(int LeftOrRightHand); public ref class CExercise : public Form { public: event WipeMouthEventHandler ^ OnWipeMouth; CExercise(void) { InitializeComponent(); } private: void InitializeComponent() { OnWipeMouth += gcnew WipeMouthEventHandler(&MouthWiper); } static void MouthWiper(int LeftOrRightHand) { } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise()); return 0; }
An application is made of various objects or controls. During the lifetime of an application, its controls regularly send messages to the operating system to do something on their behalf. These messages are similar to human messages and must be processed appropriately. Since most of the time more than one application is running on the computer, the controls of such an application also send messages to the operating system. As the operating system is constantly asked to perform these assignments, because there can be so many requests presented unpredictably, the operating system leaves it up to the controls to specify what they want, when they want it, and what behavior or result they expect. These scenarios work by the controls sending events. Events in the .NET Framework are implements through the delegates. The most common events have already been created for the objects of the .NET Framework controls so much that you will hardly need to define new events, at least not in the beginning of your GUI programming adventure. Most of what you will do consists of implementing the desired behavior when a particular event fires. To start, you should know what events are available, when they are needed, how they work, and what they produce. To process a message, it (the message) must provide at least two pieces of information: What caused the message and what type of message is it? Both values are passed as the arguments to the event. Since all controls used in the .NET Framework are based on the Object class, the first argument must be an Object type and represents the control that sent the message. As mentioned already, each control sends its own messages when necessary. Some messages are unique to some controls according to their roles. Some other messages are common to various controls, as they tend to provide similar actions. To manage such various configurations, the .NET Framework considers the messages in two broad categories. Some messages don't require much information. This type of message would be sent without much detailed information. This type of message is carried by an argument of type EventArgs passed as the second parameter of the event. Some messages must be accompanied by additional information. When a message must carry additional information, the control that sent the message specifies that information by the name of the second argument. Because there are various types of messages like that, there are also different types of classes used to carry such messages. We will introduce each class when appropriate.
Although there are different means of implementing an event, there are three main ways you can 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's code, ready to receive your instructions. Another technique you can use consists of displaying the form 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. You can also manually code an event. To do this, first define the method that will carry the event. Here is an example: public ref class CExercise : public Form { public: CExercise(void) { InitializeComponent(); } private: void InitializeComponent() { } Void WasClicked(Object ^ sender, EventArgs ^ e) { Close(); } }; Then, add the control if it is not added yet. Use the += operator to assign the type of delegate that handles the event, passing as the second argument the name of the method defined above. This assignment must be performed on the name of the event that will be fired. Here is an example: #pragma once #include <windows.h> #using <System.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Windows::Forms; public ref class CExercise : public Form { private: Button ^ btnClose; public: CExercise(void) { InitializeComponent(); } private: void InitializeComponent() { btnClose = gcnew Button; btnClose->Text = L"Close"; btnClose->Click += gcnew EventHandler(this, &CExercise::WasClicked); Controls->Add(btnClose); } private: void WasClicked(Object ^ sender, EventArgs ^ e) { Close(); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; }
|
|
||
Home | Copyright © 2007-2013, FunctionX | |
|