Messages and Events of Windows Controls
We saw that you can add Windows controls to a work area or a form to help you interact with the computer. When a control is used, it must communicate with the operating system. For example, when you click, the object that was clicked must inform the operating system that it has been clicked. This is the case for every control used in an application. Because a typical application can involve many controls, a mechanism was designed to manage this.
To communicate its intention to the operating system, a Windows control must compose a message and deliver it (to the operating system).
In order to make a message clear, the control that wants to send it must provide three important pieces of information:
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 work area or to 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.
To interact with the computer, one of the most usually performed actions is to click. The mouse is equipped with two buttons. The most clicked button is the left one.
Because the action simply consists of clicking, when you press this button, a simple event, called Click is sent or fired. When you press the (left) button on the mouse, the mouse pointer is usually on a Windows control. Based on this, the control that is clicked "owns" the event and must manage it. Therefore, no detailed information is provided as part of the event. The operating system believes that the control that fired the event knows what to do. For this reason, whenever you decide to code an OnClick event, you should make sure you know what control sent or fired the event. This is (one of) the most common events of Windows controls.
Another common action you perform on a control may consist of double-clicking it. This action causes the control to fire an event known as DblClick.
Just as an application can have many forms, a form can be equipped with various controls. Such is the case for any data entry form. On a form that is equipped with many controls, only one control can be changed at a time. Such a control is said to have focus. To give focus to a control, you can click it or can keep pressing Tab until the desired control indicates that it has focus. In a form with many controls, the control that has focus may display a caret or a dotted line around its selection or its caption.
When a form or a control receives focus, it fires the Enter event. We mentioned that a user can give focus to a control by clicking it. If the control is text-based, then a caret blinking in the control indicates that the control has focus.
The Enter event does not take any argument:
Private Sub TextBox1_Enter() End Sub
After using a control, you can switch to another control either by clicking another or by pressing Tab. This causes the focus to shift from the current control to another. If the focus shifts to another control, the control that had focus fires an Exit event.
The Exit event takes one argument, :
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) End Sub
Word processing consists of manipulating text and characters on your computer until you get the fantastic result you long for. To display these characters, you press some keys on your keyboard. If the application is configured to receive text, your pressing actions will display characters on the screen. The keyboard is also used to perform various other actions such as accepting what a dialog box displays or dismissing it.
When you press the keys on a keyboard, the control in which the characters are being typed sends one or more messages to the operating system. There are three main events that Microsoft Windows associates to the keyboard.
KeyDown: When you press a key on the keyboard, an event called KeyDown is fired. The KeyDown event takes two arguments:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _ ByVal Shift As Integer) End Sub
KeyUp: When you release a key that was
pressed, an even called KeyUp fires.
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, _ ByVal Shift As Integer) End Sub
KeyPress: The KeyPress event fires if the key you pressed is recognized as a character key; that is, a key that would result in displaying a character in a document.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) End Sub
Some keys on the keyboard don't display anything on a document. Instead, they perform (only) an action. Examples of such keys are Enter, Tab, Esc. Therefore, if you mean to find out what key you pressed, use the KeyDown event instead of the KeyPress event, even though you will have pressed a key.
A mouse is equipped with buttons, usually two, that you press to request an action. Compared to the keyboard, the mouse claims many more events that are directly or indirectly related to pressing one of its buttons.
When you press one of the buttons on the mouse, an event, called MouseDown fires. This event carries enough information as three arguments.
Private Sub txtFirstName_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
When you release a button that was pressed on the mouse, a new event fires. This event is called MouseUp. It provides the same types of information as the MouseDown event:
Private Sub txtFirstName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
The MouseMove event is sent while you are moving the mouse on a control. It provides the same pieces of information as the MouseDown and the MouseUp events:
Private Sub txtFirstName_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) End Sub
One of the most important messages of a text box occurs when its content changes. That is, when the text it has is deleted, added to, or edited. When you click in a text box control and start typing it or change its text, the control fires a Change event.
|Previous||Copyright © 2008 Yecol.com||Next|