Consulting

Results 1 to 4 of 4

Thread: Solved: finding if two controls overlap

  1. #1

    Solved: finding if two controls overlap

    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:
    [vba]Function isOverlapped(ctl1 as MSForms.Control, ctl2 as MSForms.Control) as Boolean

    <your code goes here>

    End Function[/vba]
    Thanks.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Give this a go:

    [vba]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[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]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
    [/VBA]

  4. #4
    Thanks guys. Both these procedures seem to work equally well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •