PDA

View Full Version : Solved: finding if two controls overlap



xltrader100
08-30-2010, 08:26 PM
Could somebody provide me with a function that takes two userform controls as passed parameters, and returns a boolean that indicates whether any parts of the two controls overlap. The controls will both be in the same container frame, and I don't care which of the two is on top.

It would look something like this:
Function isOverlapped(ctl1 as MSForms.Control, ctl2 as MSForms.Control) as Boolean

<your code goes here>

End Function
Thanks.

Ken Puls
08-30-2010, 09:38 PM
Give this a go:

Function isOverlapped(Ctl1 As MSForms.Control, Ctl2 As MSForms.Control) As Boolean
Dim Ctl1_TopLeft As Long
Dim Ctl1_BottomRight As Long
Select Case Ctl2.Top
Case Is = Ctl1.Top
'Control's tops are at same point
Select Case Ctl2.Height
Case Is > 0
'Check left position
Select Case Ctl2.Left
Case Is = Ctl1.Left
'Left and top are at same point
If Ctl2.Width > 0 Then GoTo Overlap
Case Is < Ctl1.Left
'Check if width of Ctl2 makes control overlap with Ctl1
If Ctl2.Left + Ctl2.Width > Ctl1.Left Then GoTo Overlap
Case Is > Ctl1.Left
'Check if width of Ctl1 makes control overlap with Ctl2
If Ctl1.Left + Ctl1.Width > Ctl2.Left Then GoTo Overlap
End Select
Case Is = 0
'Line, so does not overlap
End Select
Case Is < Ctl1.Top
'Control 2 is lower than Control 1
Select Case Ctl1.Top + Ctl1.Height
Case Is > Ctl2.Top
'May overlap, check left/width
Select Case Ctl2.Left
Case Is = Ctl1.Left
'Left and top are at same point
If Ctl2.Width > 0 Then GoTo Overlap
Case Is < Ctl1.Left
'Check if width of Ctl2 makes control overlap with Ctl1
If Ctl2.Left + Ctl2.Width > Ctl1.Left Then GoTo Overlap
Case Is > Ctl1.Left
'Check if width of Ctl1 makes control overlap with Ctl2
If Ctl1.Left + Ctl1.Width > Ctl2.Left Then GoTo Overlap
End Select
Case Else
'Either borders touch only, or don't touch at all, so does not overlap
End Select
Case Is > Ctl1.Top
'Control 2 is higher than Control 1
Select Case Ctl2.Top + Ctl2.Height
Case Is > Ctl1.Top
'May overlap, check left/width
Select Case Ctl2.Left
Case Is = Ctl1.Left
'Left and top are at same point
If Ctl2.Width > 0 Then GoTo Overlap
Case Is < Ctl1.Left
'Check if width of Ctl2 makes control overlap with Ctl1
If Ctl2.Left + Ctl2.Width > Ctl1.Left Then GoTo Overlap
Case Is > Ctl1.Left
'Check if width of Ctl1 makes control overlap with Ctl2
If Ctl1.Left + Ctl1.Width > Ctl2.Left Then GoTo Overlap
End Select
Case Else
'Either borders touch only, or don't touch at all, so does not overlap
End Select
End Select
Exit Function
Overlap:
isOverlapped = True
End Function

mikerickson
08-30-2010, 10:34 PM
Function Overlap(aCtrl As Object, bCtrl As Object) As Boolean
Dim hOverlap As Boolean, vOverlap As Boolean

hOverlap = (bCtrl.Left - aCtrl.Width < aCtrl.Left) And (aCtrl.Left < bCtrl.Left + bCtrl.Width)
vOverlap = (bCtrl.Top - aCtrl.Height < aCtrl.Top) And (aCtrl.Top < bCtrl.Top + bCtrl.Height)
Overlap = hOverlap And vOverlap
End Function

xltrader100
08-31-2010, 12:59 AM
Thanks guys. Both these procedures seem to work equally well.