Solved

# same scale for picturebox and HScroll1 and VScroll1

Posted on 2005-04-17
312 Views

Hi,

I have a picturebox that i place objects on it (for example: buttons, label, etc). When I move the object to the bottom right corner of the picturebox, I want it the scrollbars to scroll down or scroll right.

I know that to move the object to the new position, I need to add its current position + value of the scrollbar. However, I might be wrong...

Also, I need to know how to have the same scale for the distance that object moves on the picturebox and the value of the scrollbar (HScroll1.Value and VScroll1.Value)...

I attached my code in case you might need it to help me.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Public Sub RoomMouseMoveEvent(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)

Dim lLeft As Integer
Dim lTop As Integer
Dim lWidth As Integer
Dim lHeight As Integer

'if LEFT mouse button pressed
If Button = KEY_LBUTTON Then

'position
lLeft = myMapForm!pnlRoom(Index).Left
lTop = myMapForm!pnlRoom(Index).Top

'dimentions
lWidth = myMapForm!pnlRoom(Index).Width
lHeight = myMapForm!pnlRoom(Index).Height

'new position
lLeft = (lLeft + x) - mOldX
lTop = (lTop + y) - mOldY

'check boundries, if we can move the room
Call CalculateNewPosition(lLeft, lTop, lWidth, lHeight)
Call SnapToGrid

'move object. Left, Top, Width, Height
myMapForm!pnlRoom(Index).Move NewX, NewY

Else
'do nothing

End If

Dim Position As POINTAPI
GetCursorPos Position
myMapConfig.myMainForm!sbStatusBar.Panels(1) = "Room Left -" + Str\$(NewX) + " Room Top -" + Str\$(NewY) _
+ "     X -" + Str\$(x) + " Y -" + Str\$(y) + "     MouseX -" + Str\$(Position.x) + " MouseY -" + Str\$(Position.y)

End Sub

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Public Sub CalculateNewPosition(ByVal lLeft As Integer, ByVal lTop As Integer, ByVal lWidth As Integer, ByVal lHeight As Integer)

Dim lRight As Integer
Dim lBottom As Integer

lRight = myMapForm.ScaleWidth - lLeft - lWidth - myMapForm.VScroll1.Width
lBottom = myMapForm.ScaleHeight - lTop - lHeight - myMapForm.HScroll1.Height

If lRight > 0 And lBottom > 0 Then

If lLeft > 0 And lTop > 0 Then

NewX = lLeft
NewY = lTop

ElseIf lLeft <= 0 And lTop > 0 Then

NewX = 0
NewY = lTop

ElseIf lTop <= 0 And lLeft > 0 Then

NewX = lLeft
NewY = 0

ElseIf lLeft <= 0 And lTop <= 0 Then

NewX = 0
NewY = 0

End If

ElseIf lRight <= 0 And lBottom > 0 Then
If lTop > 0 Then

NewX = myMapForm.ScaleWidth - lWidth - myMapForm.VScroll1.Width
NewY = lTop

ElseIf lTop <= 0 Then

NewX = myMapForm.ScaleWidth - lWidth - myMapForm.VScroll1.Width
NewY = 0

End If

ElseIf lBottom <= 0 And lRight > 0 Then

If lLeft > 0 Then

NewX = lLeft
NewY = myMapForm.ScaleHeight - lHeight - myMapForm.HScroll1.Height

ElseIf lLeft <= 0 Then

NewX = 0
NewY = myMapForm.ScaleHeight - lHeight - myMapForm.HScroll1.Height

End If
'we are beyond right and bottom boundaries (bottom, right corner)
ElseIf lRight <= 0 And lBottom <= 0 Then

NewX = myMapForm.ScaleWidth - lWidth - myMapForm.VScroll1.Width
NewY = myMapForm.ScaleHeight - lHeight - myMapForm.HScroll1.Height

End If

NewX = NewX + myMapForm.HScroll1.Value
NewY = NewY + myMapForm.VScroll1.Value

End Sub
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks a lot!

0
Question by:TungVan

LVL 1

Expert Comment

http://vbnet.mvps.org/code/forms/scrollviewport.htm

Hope it helps!
0

LVL 1

Accepted Solution

Oh yeah, this link tells you how to maintain aspect ratio while scrolling:

http://vbnet.mvps.org/code/subclass/aspectratio.htm
0

## Featured Post

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…