Freetutes.com - Free Visual basic Tutorial website on the web

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Sponsored Links

Mouse Events In Visual Basic

 
Visual Basic responds to various mouse events, which are recognized by most of the controls. The main events are MouseDown, MouseUp and MouseMove. MouseDown occurs when the user presses any mouse button and MouseUp occurs when the user releases any mouse button. These events use the arguments button, Shift, X, Y and they contain information about the mouse's condition when the button is clicked.

The first argument is an integer called Button. The value of the argument indicates whether the left, right or middle mouse button was clicked. The second argument in an integer called shift. The value of this argumnet indicates whether the mouse button was clicked simultaneously with the Shift key, Ctrl key or Alt key. The third and fourth arguments X and Y are the coordinates of the mouse location at the time the mouse button was clicked. As the Form_MouseDown( ) is executed automatically whenever the mouse button is clicked inside the Form's area the X, Y co-ordinates are referenced to the form.

Positioning a control

MouseDown is the commonly used event and it is combined wiyth the move method to move an Image control to different locations in a Form. The following application illustrates the movement of objects responding to move events. it makes use of two OptionButton Controls, two image controls and a CommandButton. The application is designed in such a way that when an OptionButton is selected, the corresponding image control is placed anywhere in the form whenever it is clicked.

Open a new standard EXE project and save the Form as Move.frm and save the project as Move.vbp Design the Form as shown below.

Object
Property
Setting

Form

 

Caption

Name

MouseDown

frmMouseDown

OptionButton

 

 

Caption

Name

Value

Credit card is selected

optCredit

True

OptionButton

 

Caption

Name

Cash is selected

optCash

Image

 

Name

Picture

imgCredit

c:/credit.jpg

Image

 

Name

Picture

imgCash

c:/cash.jpg

The follwoing code is entered in the general declarations section of the Form.

Option Explicit

The following code is entered in the Form_MouseDown( ) event

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If optCredit = True Then
     imgCredit.Move X, Y
  Else
     imgCash.Move X, Y
End If
End Sub

Run the application by keying in F5. You can notice that when the mouse is clicked on the form somewhere, the selected image moves to that clicked location. This is shown in the below figure.

( Download the source code )

Previous Page | Table of Contents | Next Page

 

 

 

   
Home | Link to Us | Partner Links | About Us | Contact Us
Copyright © 2007-2008 Freetutes.com | All Rights Reserved