Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
-
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
-
Error Handling (2)
-
VB6 Database

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

Previous Page | Table of Contents | Next Page

Mouse Events In Visual Basic 6

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 | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved