Public Shared Sub EnsureOnScreen(ByRef aControl As Control)
Dim mainLeft As Integer
Dim mainRight As Integer
Dim mainTop As Integer
Dim mainBottom As Integer
Dim formLeft As Integer
Dim formRight As Integer
Dim formTop As Integer
Dim formBottom As Integer
Dim moveLeft As Integer
Dim moveRight As Integer
Dim moveUp As Integer
Dim moveDown As Integer
Dim upperLeft As Point
Dim lowerRight As Point
upperLeft = aControl.PointToScreen(New Point(0, 0))
lowerRight = aControl.PointToScreen(New Point(aControl.Width, aControl.Height))
formLeft = upperLeft.X
formTop = upperLeft.Y
formRight = lowerRight.X
formBottom = lowerRight.Y
mainLeft = 0
mainTop = 0
mainBottom = Screen.PrimaryScreen.Bounds.Bottom
mainRight = Screen.PrimaryScreen.Bounds.Width
If formLeft < mainLeft Then
moveRight = mainLeft - formLeft
End If
If formRight > mainRight Then
moveLeft = formRight - mainRight
End If
If formTop < mainTop Then
moveDown = mainTop - formTop
End If
If formBottom > mainBottom Then
moveUp = formBottom - mainBottom
End If
upperLeft = aControl.Location
upperLeft.X = upperLeft.X - moveLeft + moveRight
upperLeft.Y = upperLeft.Y + moveDown - moveUp
aControl.Location = upperLeft
End Sub