Originally published in FoxTALK March 1998
The Button on the Mouse Goes Up and Down
Last month we began looking at managing the mouse actions using the various mouse events. This month we will look at three particular mouse events in some detail and figure out how they work and, more importantly, what we might do with them.
So now we understand a bit about the MouseMove event and we saw that we can use it to track, and react, to the movement of the mouse pointer over controls. There are a number of different button configurations that a mouse may have. There may be 1, 2 , or 3 buttons referred to as Left, Middle, and Right. Newer mice may have a wheel between the right and left buttons. Visual FoxPro 3.0 provides events for many of these buttons, and version 5.0 adds events for the MouseWheel as well.
These mouse events will fire based on the user’s actions with the mouse. If we want to take full advantage of these events we need to understand the sequence in which they fire and how we can affect them with our code.
The sample form for this article is shown in figure 1 below;
The command button has code in the MouseDown, MouseUp and Click events that writes a string to the editbox for each event. Figure 2 shows the result of clicking on the button.
You can see in the editbox that the sequence of the events is MouseDown, MouseUp, and then Click. Although we didn’t show this, the DoubleClick would follow the Click event when a DoubleClick takes place. So, what’s the point of this, why three events for a simple mouse click? The answer is because a mouse click is three events, first the mouse button goes down, then it comes up, and then the click fires.
What can we do with this information? In a situation where you wanted something to happen whenever the user clicked the control, and there was also code that may or may not be executed according to the situation, you could take advantage of the fact that the mouse action fires multiple events.
In the sample form the button’s MouseUp event has the code below;
LPARAMETERS nButton, nShift, nXCoord, nYCoord
THISFORM.Edit1.Value = THISFORM.Edit1.Value + "MouseUp" + CHR(13) + CHR(10)
Figure 3 shows the results of clicking the button when the checkbox is checked.
The above code in the MouseUp for the button checks to see if the checkbox is checked and if it is it executes a NODEFAULT command. The NODEFAULT stops Visual FoxPro from doing the default behavior of the MouseUp, which includes firing the Click event. We were able to take action on the mouse activity but stop the firing of the Click event. When the checkbox is not checked the NODEFAULT is not executed and the Click event fires as usual.
Another possible use of these events is demonstrated in the editbox itself. There is code in the MouseDown and the MouseUp events of the editbox that respectively set the MousePointer property to 4 (an Icon shape) and then back to 0 (the default pointer). Figure 4 shows the sample form with the mouse button pressed down over the editbox.
In the last two columns we examined a number of the mouse events and saw some ideas of how to use them. One of the important things we learned is that it is important to clearly understand what each event is and when it fires. It is also critical to understand the sequence in which the events fire. This is not only true of the mouse events but of all of the events methods in Visual FoxPro. To make the best use of these events you need to understand them clearly.