First you will need to create a form with at least 1 element in it (label, textbox, picturebox, etc..). Once that is complete, make sure you are viewing and editing the code (in VB.NET Express 2005 right click in the form and click "view code").
We are going to need a few variable to begin with. 3 to use as "switches" (on/off or true/false in this case), 2 to hold the position of the mouse (x/y coordinates), and 2 to hold the position of the mouse where it was clicked over the element (again, x/y coordinates). This last set is to eliminate any left or right jumping of the element when clicked, and allow for a smooth click and drag flow. (for beginners this all goes in the Public Class Form1 section)
CODE
' These will be our switches
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
' These will hold the mouse position
Dim HoldLeft As Integer
Dim HoldTop As Integer
' These will hold the offset of the mouse in the element
Dim OffLeft As Integer
Dim OffTop As Integer
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
' These will hold the mouse position
Dim HoldLeft As Integer
Dim HoldTop As Integer
' These will hold the offset of the mouse in the element
Dim OffLeft As Integer
Dim OffTop As Integer
Let me explain the switches real quick. The first (Go) will be the switch that tells the system when the mouse is pressed and ready for dragging. The second (LeftSet) and third (TopSet) will tell the system whether or not we have captured the original point where the mouse was clicked.
Next we need to set our switches. They will all need to be defaulted to off when the mousebutton is UP (as in not clicked). Obviously if you aren't clicking on the object you aren't trying to move it.
CODE
Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles obj1.MouseUp
Go = False
LeftSet = False
TopSet = False
End Sub
Go = False
LeftSet = False
TopSet = False
End Sub
Now that they are all set, when do we need to tell the system that we are trying to move the object? When the mouse button is pressed. Since all we have to do for now is tell the system that the mouse is down, we only need to flip the first switch (Go)
CODE
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles obj1.MouseDown
Go = True
End Sub
Go = True
End Sub
Now the system knows when the mouse is clicked or not, but we need to make the element "follow" the mouse. I will explain the steps before the code
1.) We need to check if the mouse is clicked or not when the mouse is moving. We will use the first switch and an IF statement
2.) We need to find out where the mouse is right now. We will store the coordinates with the HoldLeft & HoldTop variables
3.) We need to capture the point where the mouse was clicked, but only one time! We will use the second and third switch and some IF statements
4.) Lastly we move the object to where the mouse is
CODE
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles obj1.MouseMove
' Check if the mouse is down
If Go = True Then
' Set the mouse position
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
' Find where the mouse was clicked ONE TIME
If TopSet = False Then
OffTop = HoldTop - sender.Top
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
LeftSet = True
End If
' Set the position of the object
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
' Check if the mouse is down
If Go = True Then
' Set the mouse position
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
' Find where the mouse was clicked ONE TIME
If TopSet = False Then
OffTop = HoldTop - sender.Top
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
LeftSet = True
End If
' Set the position of the object
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
Notice how I used sender? This is so that only the element you click is moved. sender is a system variable that returns the name of the element that is returning values. You can use this code once and set as many objects or elements as you want to be movable.
To add another object to be movable just add objname.event to all three of the handles
- objname.MouseDown
- objname.MouseUp
- objname.MouseMove