In order to make a message clear, the control that
wants to send it must provide three important pieces of information:
- WHO sent the message? When a control sends a message, it must
identify itself because there can be many controls sending different
messages at the same time. The operating system will need to know
where a message came from. This is one of the reasons why every
control must have a name. Also because each message is particular to
the control that sent it, the message is considered a private matter.
Based on this, the code of a message starts with Private Sub,
followed by the name of the control that is sending the message:
Private Sub ControlName
- WHAT message? When a control is sending a message, it must specify
the type of message. A control can be able to send various types of
messages. For example, when a control gets clicked, it sends a Click
message. If the same control receives focus but the user presses a
key, the control sends a keyboard-related message. When the mouse
passes over that same control, its sends a different type of message.
Every message a control can send has a name. To see the types of
message available for a particular control, open Microsoft Visual
Basic. In the Object combo box, select the name of the control. Then,
click the arrow of the Procedure combo box:
By convention, the name of the message follows the name of the control
but they are separated with an underscore. It would appear as:
Private Sub ControlName_Push
- Arguments: An argument is additional information needed to process a
message. When a control sends a message, it may need to accompany it
with some information. For example, if the user positions the mouse on
a control and clicks, the operating system may need to know what
button of the mouse was used to click. On the other hand, if the user
selects an object and starts dragging, the operating system may need
to know if a key such as Shift or Ctrl was held down while the user
was dragging.
An additional piece of information that the control provides is called
an argument. While some messages may need to provide only one piece of
information, some messages would require more than one argument. Some
other messages don't need any additional information at all: the name
of the message would completely indicate how the message must be
processed.
The arguments of a message are provided in parentheses. They would
appear as:
Private Sub ControlName_Push(Argument1, Argument2,
Argument_n)
After specifying the message, you can type code that
tells the operating system what to do to process the message. To indicate
the end of the code that relates to a message, type End Sub
Private Sub ControlName_Push(Argument1, Argument2, Argument_n)
End Sub
As mentioned earlier, a message must be composed and
sent. The action of sending a message is called an event. It is also said
that the controls "fires" an event. Based on the above
descriptions, to compose and send a message, in the Object combo box, you
can select the name of the control that will send the message, then select
the desired message in the Procedure combo box. When you do this,
Microsoft Visual Basic will write the first line that specifies the name
of the control, the name of the event, its arguments if any, and would
write End Sub for you. You can then enter the necessary code between those
two lines.
Most Windows control have a special event referred to
as the default. This is the even that is the most obvious that the control
can fire. For example, when you think of a button, the first action that
comes to mind is Click. For this reason, Click is the default event of a
button. If you add a control to a spreadsheet or a form and double-click
the control, its default event would be invoked and the skeleton of that
event would be written in the corresponding module. If you don't want to
use that event or to fires another event for the same control, you can
simply select the event in the Procedure combo box.
|