The magazine of the Melbourne PC User Group

Seeing the Basics: Control events
Tony Stevenson
mkdsoftware@trump.net.au

Control events execute code (that a software developer has written) when a user interacts with a control. For example, when a command button is clicked the code associated with that control's Click event is processed.

The best way to learn about control events is to experiment.

Add a command button to a form and insert a line of code into its Click event as follows:

Private Sub Command1_Click()
Print "In Click Event For Command1 Button"
End Sub


Running this program and clicking the command button results in the expected behaviour - he contents of the Print statement appear in the top left hand corner of the project's form.

Now add the following code to the command button's MouseUp event.

Private Sub Command1_MouseUp
  (Button As Integer,
  Shift As Integer,
  X As Single,
  Y As Single)
Print "In MouseUp Event For Command1 Button"
End Sub

Run the program and click the command button. As expected the contents of both Print statements appear on the form.

Now run the program once more. However, this time click the mouse on the button, but keep the mouse button depressed while you move the cursor off the command button surface. Now release the mouse button.

Can you predict what will happen?

When the button is clicked, the command button image will appear depressed. When the mouse cursor is moved off the button, the command button image will return to its normal position (making it look like the button was pushed in and then released). When the mouse button is finally released, only the contents of the Print statement in the MouseUp event will be printed (despite the "pushed and released" appearance of the command button).

What is the significance of this discussion? There are two points to note:
  • There are events for Click, MouseUp, MouseDown, MouseMove, etc. providing VB programmers with great coding flexibility. For example, by using a combination of the MouseDown, MouseUp, and MouseMove events of a form it is possible to implement an elementary paint application. (See the example in the VB online help topic "MouseUp event" for the code to do this.)
  • Care must be taken when coding control events. It is possible to unwittingly introduce errors where event code gets executed (or alternatively not executed) in ways that were not originally intended.
Which mouse button was pressed?

It is possible to extend the code attached to the command button to test which mouse button (left, middle, or right) was pressed.

In the MouseDown event for a command button one of the parameters passed is the Button parameter. The value of this integer parameter can be tested to determine which mouse button was used.

Add the following code to the MouseDown event of the command button.

Private Sub Command1_MouseDown
  (Button As Integer,
   Shift As Integer,
   X as Single,
   Y As Single)
Select Case Button
Case 1
   Print "Left button clicked"
Case 2
   Print "Right button clicked"
Case 4
   Print "Middle button clicked"
Case Else
   MsgBox "Fourth Button?"
   End
End Select
End Sub


Notice the error message after the Case Else. It is a good idea to practice defensive programming. It is unlikely that a mouse with more than three buttons will ever be manufactured. But if it does happen then this code will detect such a situation. Make it a standard to always include a Case Else (along with a suitable message and action) for all Select statements that you code.

Whilst the above code works, a more professional approach is to replace the integer constants with the predefined mouse button constants that are supplied with VB. Doing this makes a program easier to read and debug (it also offers a level of protection if the designers of VB reassign the numerical values of these constants). To see what these constants are, open the VB Object Browser (click the appropriate button on the design toolbar or use the menu options View, Object Browser).

Once in the Browser, select the entry VB - Visual Basic objects and procedures from the Libraries / Projects combo box. From the Classes / Modules list box select the entry MouseButtonConstants. Rewrite the Select statement as follows:

Select Case Button
Case vbLeftButton
   Print "Left button clicked"
Case vbRightButton
   Print "Right button clicked"
Case vbMiddleButton
   Print "Middle button clicked"
Case Else
   MsgBox "Fourth Button?"
   End
End Select


Note: To find the descriptions of other miscellaneous VB constants, search for the topic miscellaneous constants in the VB Help system.

Help when coding

It is easy to forget VB syntax, however online help is always close at hand. For example, if you want to code a Select Case statement but can only remember that it contains the "case" key word (and nothing else) then do the following.

Type the word "case" in a code window and highlight it using the mouse. Now press the function key F1. If VB can recognise what you have typed (which it can on this occasion) then an appropriate help topic will be displayed. Alternatively, if you could only remember the word "select" (and you repeat this process), VB reacts slightly differently. Because it could not recognise an unambiguous help topic, VB instead displays a list of multiple help topics. In this case there are two entries for the select object, one in the VB library and the other in the VBA library (this is for version 4.0 of VB). Selecting one of these items will cause the corresponding help information to be displayed.

This context method of obtaining help is available in both versions 3.0 and 4.0 of VB.

Click event for a list box

If writing code for the click event of a list box, be aware that this code will also be executed whenever the keyboard's up or down arrow keys are used.

However, you might be surprised by the behaviour of the right and left arrow keys in this situation. The right arrow selects the next item in the list, while the left arrow selects the previous list item. For both of these keys, the code in the click event is executed.

Lastly, the keyboard's Home and End keys select the first and last items in the list respectively. Again, in each case the click event code is executed.

Reprinted from the September 1996 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]