PDA

View Full Version : [Tutorial] Adding Functionality to your Visual Basic programs with the Mouse


xilica
05-27-2002, 11:00 PM
Hey everyone,

note: You should have some prior experience upon doing this tutorial.

As I said before...I am on a fresh start and its a straightened era
from here on out!!!
Just writing a tutorial...by the way: For right now I am gonna right tutorials for the programming language I know - Visual Basic.


Adding the Interface:


Once you load up Visual Basic click Standard EXE.

Change the Forms properties to these:

1. Name: frmMouse
2. Caption: Mouse Control
3. Height: 4200
4. Width: 5200

The width and height resize the form. The name is for clearer coding for when you have situations with multiple objects which are the same. It is a good habbit to change the name according to the type of object it is. The caption is the name that appears on the top bar of the program.


Now, drag a Text Box anywhere on the Form. Change its properties to these:

1. Name: txtMouse
2. Font: Bold, 14 point
3. Height: 1095
4. Left: 960
5. MultiLine: True
6. Text: (Delete whatever text is here) It is usually Text1
7. Top: 1920
8. Width: 3255

The width and hidth resize the text box. The Font is for what size and style of the text which appears in the text box. The Left and Top position the textbox on the form. The text is the text which appears in the textbox when you start the program by default. Multiline allows for easy wordwrapping.


Now, drag an Image Box anywhere on the Form. Change its properties to these:

1. Name: imgMouse
2. Left: 220
3. Picture: C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Misc\BULLSEYE.ICO
4. Top: 600

The Picture is the icon which you want to import as the image. Left and Top are where the image box will be located on the form.


Congrats...you have just made the interface. Before you add the code feel free to grab a Coke and chill.


Adding the Code:


Doublie-click on the Form. Add this code:


Private Sub Form_Click()

txtMouse.Text = "You clicked the form"

End Sub


Double-click the Form. Notice at the top there are two drop-down menus. On the drop down menu on the right select: DblClick. The drop-down menu on the right is just for selecting the object. That is why Form is selected.


Private Sub Form_DblClick()

txtMouse.Text = "You double-clicked the form"

End Sub


Double-click on the Form. At the right drop-down menu select: Mouse Down. Add this code:


Private Sub Form_MouseDown(intButton As Integer, _
intshift As Integer, sngX As Single, sngY As Single)


txtMouse.Text = "Clicked over the form at " & sngX & ", " & sngY

End Sub


Double-click on the Form. At the right drop-down menu select: MouseMove. Add this code:


Private Sub Form_MouseMove(intButton As Integer, _
intshift As Integer, sngX As Single, sngY As Single)

txtMouse.Text = "Moving the mouse..."

End Sub


Doube-click on the Bullseye image. Add this code:


Private Sub imgMouse_Click()

txtMouse.Text = "You clicked the image"

End Sub


Double-click on the Bullseye image. Change the right drop-down menu to DblClick. Add this code:


Private Sub imgMouse_DblClick()

txtMouse.Text = "You double-clicked the image"
End Sub


Double-click the Bullseye image. Change the right drop-down menu to MouseDown. Add this code:


Private Sub imgMouse_MouseDown(inButton As Integer, intshfit As _
Integer, sngX As Single, sngY As Single)

txtMouse.Text = "Clicked over the image at " & sngX & ", " & sngY

End Sub


Double-click the Bullseye image. Change the right-drop-down menu to MouseMOve. Add this code:


Private Sub imgMouse_MouseMove(inButton As Integer, intshfit As _
Integer, sngX As Single, sngY As Single)

txtMouse.Text = "You moved over the image"

End Sub



Analyzing the Code:



As you see you have added many functions to the two objects. When you change each ones function, it changes the look of the code. As you see many of these functions are just like human language. For example, MouseDown and MouseMouse will signify whether a user clicks or moves the mouse. The reason each one begins with "txt.Mouse.Text" is because that is where those output lines will go depending on which function you are playing with.


Here is a picture of what your program should look like done
correctly: http://www.tmstadium.com/extrastuff/VisualBasicMouseControl.jpg



Credits:

Nafae - For hosting this tutorial on his Forum. I appreciate it.

Mydster - For hosting my picture on his website. Much appreciated.

Ice Water - For queching my thirst while I was typing this Tutorial.
Thanks man!!!




Also, feel free to posts any comments regarding this Tutorial

mrbojangle
05-28-2002, 12:23 AM
nice way to explain it :o)

xilica
05-28-2002, 12:24 AM
Hey bojangle,
Thanks...
By the way, what is the depth of your knowledge in Visual Basic?