Using the Mouse in Visual Basic

There are actually two parts to being a "mouse" expert. The first part is simply using mouse movements, including button clicks, in your program. You'll find that handling mouse movements is very simple. Any VB programmer can learn to respond to the movement of the mouse or the click of a mouse button.

The second part is using the built-in drag-and-drop features of VB. I cover drag and drop in the next section of the tutorial.

Mouse Movement and Mouse Button Clicks
Incredible as it may sound, you only need to understand 3 events to become a MouseMaster (ok, it's not as useful a title as an MSCD Engineer, but the title comes free with completion of this tutorial!). The key to this simplicity is that a mouse is not an object. It has no properties and it has no methods. The recognition of mouse activity (move/click) is actually built into all of the other controls! If you look at the controls that come with VB (or commercial controls) you'll see the following 3 events:

It doesn't take much examination to realize that the arguments for each mouse event are exactly the same. This greatly simplifies our discussion here and makes your learning process much simpler!

First, let's make sure we know what is going on inside VB. If you press and release a mouse button, BOTH the MouseDown and MouseUp events will be executed, for the control over which the mouse cursor is positioned. Make sure you understand that. Mouse events happen for the control under the mouse. This is true even if, after pressing a button, you move the mouse of a control before releasing the button. In that case the MouseUp event still takes place for the control over which the mouse button was pressed.

One thing to note about the mouse events. While the MouseMove event will happen by moving a mouse, it will also happen when you click a mouse button.

Remember also, that clicking a mouse button or moving a mouse does not automatically make anything happen inside your program. Unless you write code that executes inside the mouse up/down/move events, your program will ignore the mouse actions.

As you can see in the arguments of the mouse events, there are only 4 variables to understand. Let's tackle each one now.

X and Y
These are the simplest of the arguments and are exactly what you think they are - the position of the mouse within the control (or the form, if the mouse cursor is not over a control). The only "trick" to remember is that X and Y are given in coordinates as defined by the .ScaleMode property of the control or form. Check out the graphics part of my tutorial for information on the ScaleMode property of an object.

The X and Y properties are simply information that you can use in your program. For example, if you were to draw a circle inside a picture control, and wanted to know if the mouse click took place inside the circle, you would examine X and Y to see if it falls within the bound of the circle. What you do with the information is up to the code that you chose to put into the event procedures.

In my experience, the X and Y are typically used to identify where to take an action, such as where to create a graphic.

Button
The Button arguments actually can tell which of the three possible mouse button(s) are pressed. The possibles values of Button are 0 through 7. The following code shows how you would use Button to to figure out what is being pressed:

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case Button
Case 7 'all three buttons are pressed
End Select
End Sub

If you simply want to know if a particular button is being pressed, and don't care about the state of any other button, then you can use this code:

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub

In all my programs I use the press of a single button for my user interface. I can't think of a single instance where I found it necessary to have my user press two buttons at once.

Here's one more point you should know. The MouseMove event does not take place for each pixel that the mouse is moved. Instead, the number of events that takes place depends on the operating speed of your PC and how loaded the processor is. The bottom line is that while the frequency with which MouseMove events are generated is reasonably high, you cannot depend on the event to take place any specific number of times, nor at any specific interval of time.

Shift
As I mentioned above, I very rarely require that my user press multiple mouse buttons to accomplish a task. I also never force him to press a key at the same time as he presses a mouse button. However, there are a number of programs which do this (Windows 9x, for example, uses CTRL-Mouse to identify a copy command). VB supports this capability by providing the Shift argument. Like the Button argument, the Shift argument also has 8 values as follows:

0 - Neither the SHIFT, CTL, or ALT keys are pressed
1 - SHIFT key is pressed
2 - CRTL key is pressed
3 - ALT key is pressed
4 - SHIFT and CRTL keys are pressed
5 - SHIFT and ALT keys are pressed
6 - CRTL and ALT keys are pressed
7 - All three keys (SHIFT, CTRL, and ALT) are pressed

A point to remember is that just because you press one of these keys while also pressing a mouse doesn't mean that anything will automatically happen. You must put the code in your events to determine what action to take based on which keys are pressed.

One last point about using the mouse before we tackle drag and drop features. You saw that the Button and Shift arguments take on values from 0 to 7. As I have mentioned before I strongly suggest that you use the built-in constants that VB offers to help make your code more readable.

For example, the code:

If Button = vbLeftButton
is more understandable than
If Button = 1
For testing the Button argument, use the vbLeftButton, vbRightButton, and vbMiddleButton constants.