![]() |
Event Implementation |
|
Introduction |
|
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. 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 concepts of delegates and events as reviewed previously. 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. 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 work, 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. Based on this, 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. As it happens, in order to perform their intended action(s), some messages do not require much information. For example, suppose your heart sends a message to the arm and states, “Raise your hand”. In this case, suppose everything is alright, the arm does not ask, “how do I raise my hand?”. It simply does because it knows how to, without any assistance. This type of message would be sent without much detailed information. In the .NET Framework, a message that does not need particular information is carried by a class named EventArgs. In the event implementation, an EventArgs argument is passed as the second parameter. Consider another message where the arm carries some water and says to the mouth, “Swallow the following water”. The mouth would need the water that needs to be swallowed. Therefore, the message must be accompanied by additional information. Consider one more message where the heart says to the tongue, “Taste the following food but do not swallow it.” In order to process this message, the tongue would need the food and something to indicate that the food must not be swallowed. In this case, the message must be accompanied by detailed pieces of 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 two main ways you can initiate its coding. If the control has a default event and if you double-click it, the designer 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. As another technique, display the form and click either the
body of the form or a control on it. Then, on the Properties window, click the
Events button
Another alternative you can use consists of displaying the form, right- clicking the form and clicking View Code. Then, in the Class Name combo box, select the name of the control:
In the Method Name combo box, select the event you want to implement:
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: Private Sub Control_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
End Sub
This event is carried by a PaintEventHandler delegate declared as follows: Public Delegate Sub PaintEventHandler ( _ sender As Object, _ e As PaintEventArgs _ ) The PaintEventArgs parameter provides information about the area to be painted and the graphics object to paint.
When using an application, one of the actions a user can perform on a form or a control is to change its size, provided the object allows it. Also, some time to time, if possible, the user can minimize, maximize, or restore a window. Whenever any of these actions occur, the operating system must keep track of the location and size of a control. For example, if a previously minimized or maximized window is being restored, the operating system must remember where the object was previously positioned and what its dimensions were. When the size of a control has been changed, it fires the Resize() event, which is a EventArgs type.
It is possible, but unlikely, that none of the available events featured in the controls of the .NET Framework suits your scenario. If this happens, you can implement your own event. To do this, you should first consult the Win32 documentation to identify the type of message you want to send. There are two main techniques you can use to create or send a message that is not available in a control. You may also want to provide your own implementation of a message.
In order to send a customized version of a Windows message from your control, you must first be familiar with the message. A message in the .NET Framework is based on the Message structure. One of the properties of this structure is Msg. This property holds a constant integer that is the message to send. The constant properties of messages are defined in the Win32 library. To send a message, you can declare a variable of type Message and define it. Once the variable is ready, you can pass it to the DefWndProc() method. Its syntax is: Protected Overridable Sub DefWndProc(ByRef m As Message) To know the various messages available, you can consult the Win32 documentation but you need a way to get the constant value of that message. Imagine you want to send a message to close a form when the user clicks a certain button named Button1. If you have Microsoft Visual Studio (any version) installed in your computer, you can open the WINUSER.H file. In this file, the WM_CLOSE message that carries a close action is defined with the hexadecimal constant 0x0010 ![]() You can then define a constant integer in your code and initialize it with this same value.
To process a Windows message that is not available for a control you want to use in your application, you can implement its WndProc() method. Its syntax is: Protected Overridable Sub WndProc(ByRef m As Message) In order to use this method, you must override it in your own class. Once again, you must know the message you want to send. This can be done by consulting the Win32 documentation. |
|
|
||
| Home | Copyright © 2008 FunctionX, Inc. | Home |
|
|
||