The MouseUp Event

The MouseUp event occurs when the user releases the mouse button. MouseUp is a useful companion to the MouseDown and MouseMove events. The example below illustrates how all three events can be used together.

The Scribble application is more useful if it allows drawing only while the mouse button is held down and stops drawing when the button is released. To do this, the application would have to respond to three actions:

MouseDown and MouseUp will tell the application to turn drawing on and off. You specify this by creating a form-level variable that represents the drawing state. Type the following statement in the Declarations section of the form code module:

Dim DrawNow As Boolean

DrawNow will represent two values: True will mean "draw a line," and False will mean "do not draw a line."

Because variables are initialized to 0 (False) by default, the application starts with drawing off. Then the first line in the MouseDown and MouseUp procedures turns drawing on or off by setting the value of the form-level variable DrawNow:

Private Sub Form_MouseDown (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   DrawNow = True
   CurrentX = X
   CurrentY = Y
End Sub

Private Sub Form_MouseUp (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   DrawNow = False
End Sub

The MouseMove procedure draws a line only if DrawNow is True. Otherwise, it takes no action:

Private Sub Form_MouseMove (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   If DrawNow Then Line -(X, Y)
End Sub

Each time the user presses a mouse button, the MouseDown event procedure is executed and turns drawing on. Then as the user holds the Mouse button down, the MouseMove event procedure is executed repeatedly as the pointer is dragged across the screen.

Note that the Line method omits the first endpoint, causing Visual Basic to start drawing at the mouse pointer's current coordinates. By default, the drawing coordinates correspond to the last point drawn; the form's CurrentX and CurrentY properties were reset in the Form_MouseDown procedure.

For More Information   See "MouseUp Event" in the Language Reference.