-
Jan 9th, 2016, 06:38 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Moving a Picturebox Horizontally with Limitations
I have a Picturebox on my Form that I'd like to be able to drag left and right but to only go so far (limitations). I have set the left limitation to 2400 and the right to 6000. It drags fine, but once it reaches the limitations, it doesn't stop. Here is my code:
Code:
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static lx As Single, ly As Single
If Button = vbLeftButton Then
If (Picture1.Left > 2399) And (Picture1.Left < 6001) Then
Picture1.Left = Picture1.Left + (X - lx)
Else
If Picture1.Left < 2400 Then Picture1.Left = 2400
If Picture1.Left > 6000 Then Picture1.Left = 6000
End If
Else
lx = X: ly = Y
End If
End Sub
It works, but when it gets to the left and right limitations, it does this:
It also randomly continues past the limitations. Anyway to correct this?
-
Jan 9th, 2016, 09:22 PM
#2
Re: Moving a Picturebox Horizontally with Limitations
I've also run into trouble figuring out these kinds of movement issues in some of my programs, and what I've found helps is to resolve them is:
- Perform and store all calculation steps in variables and check the results using DEBUG.PRINT statements
- Add DEBUG.ASSERTS to pause execution at unexpected conditions - this gives you a chance to interrupt your assumptions and re-evaluate your approach early.
- Use constants for "magic numbers" like Min/Max values - sometimes you change your mind about a range, and miss changing the magic numbers everywhere in your code - this can lead to unexpected results and be a real time sink.
This code *should* work as required and also demonstrates some of the principles listed above. If it doesn't work as required, then the debugging information should help you diagnose the source of the problem. If not, then post your new code and I'll take a look
Code:
Option Explicit
Private Const mc_MinLeft As Long = 2400
Private Const mc_MaxLeft As Long = 6000
Private Sub Form_Load()
If mc_MinLeft > mc_MaxLeft Then
' Minimum left value must be less than maximum left value!
Debug.Assert False ' Stop here in debugger to warn developer of unexpected condition
Err.Raise vbObjectError, , "Minimum left value must be less than maximum left value!"
End If
' Make sure PB is within min/max bounds
If Me.Picture1.Left < mc_MinLeft Then Me.Picture1.Left = mc_MinLeft
If Me.Picture1.Left > mc_MaxLeft Then Me.Picture1.Left = mc_MaxLeft
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static lx As Single, ly As Single
Dim l_Left As Single
Dim l_Delta As Single
Dim l_NewPos As Single
If Button = vbLeftButton Then
' Left mouse button is down, calculate the difference between the last position and the current position
l_Delta = X - lx
If l_Delta <> 0 Then
' Mouse has moved
Debug.Print "Mouse has moved " & l_Delta & " from original position."
' Calculate new position
l_NewPos = Picture1.Left + l_Delta
Debug.Print "New position will be " & l_NewPos
If (l_NewPos >= mc_MinLeft) And (l_NewPos < mc_MaxLeft) Then
' New position is within our min/max range, so reposition the PB
Debug.Print "New position is within Min/Max bounds of " & mc_MinLeft & "/" & mc_MaxLeft & " - reposition PB."
Me.Picture1.Left = l_NewPos
Else
' New position is outside of our min/max range
' Test if we should move the PB to the min or max value
Debug.Print "New position is NOT within Min/Max bounds of " & mc_MinLeft & "/" & mc_MaxLeft & ". Check if we should move PB to MIN/MAX boundary."
If l_NewPos < mc_MinLeft Then
' The new position is less than our minimum position, so set the PB to the minimum position
If Me.Picture1.Left > mc_MinLeft Then
' The PB.Left is greater than our min position, so move it to our max position
Debug.Print "Current PB left position OUTSIDE min/max bounds - set it to minimum left boundary."
Me.Picture1.Left = mc_MinLeft
Else
Debug.Print "Current PB left position is within min/max bounds - leave everything alone."
End If
Else
' The new position is greater than our maximum position
If Me.Picture1.Left < mc_MaxLeft Then
' The PB.Left is less than our max position, so move it to our max position
Debug.Print "Current PB left position OUTSIDE min/max bounds - set it to maximum left boundary."
Me.Picture1.Left = mc_MaxLeft
Else
Debug.Print "Current PB left position is within min/max bounds - leave everything alone."
End If
End If
End If
End If
ElseIf Button = 0 Then
' No button down, record current mouse position for calculating movement when button is down
lx = X: ly = Y
End If
End Sub
-
Jan 9th, 2016, 10:20 PM
#3
Thread Starter
Addicted Member
Re: Moving a Picturebox Horizontally with Limitations
That works perfectly. Now, to figure out why 1 / 3600 is giving me zero.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|