61391

Check if a control is “touching” another control

I am trying to check if a Windows Forms control is "touching" another Windows Forms control within the same Form.

Example: There are two buttons inside a Form. Lets say that the two buttons are moveable within the boundaries of the Form. How would one check if the two button are touching (or any System.Control for that matter)?

How can this be checked?

Answer1:

You can check the control Bounds against other controls and check if they have any intersct.

// if your first control is specified you can use the following code foreach (Control c2 in Controls) { if (!c2.Equals(c1) && c2 is Button /* if you want it to be just buttons */ && c1.Bounds.IntersectsWith(c2.Bounds)) { // c1 has touched c2 } }

If all controls can move and you want to see when they touch each other you can use the code below:

foreach (Control c1 in Controls) { foreach (Control c2 in Controls) { if (!c2.Equals(c1) && c1.Bounds.IntersectsWith(c2.Bounds)) { // c1 has touched c2 } } }

Answer2:

Maintain display rectangle of all parent controls. For example if there is a group box maintain display rectangle of that and not of controls inside it. While moving a control, check if current display rectangle overlaps with another.

Recommend

  • reformat multidimensional array based on value
  • make a particular element to be first in array
  • MVC - Dynamically loading Partial Views
  • How to insert an Image in WORD after a bookmark using OpenXML
  • QObject::findChild() returns None without obvious reason
  • Sliding transition animation between Screens in Caliburn Micro
  • Why are views not counted if you embed a youtube iframe dynamically using jquery?
  • How do i find all references to a user control
  • TextBox AutoCompleteStringCollection Suggest
  • Java : How to tint this PNG programmatically?
  • Unable to play media with vlc ocx
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • Enumerating Controls on a Form
  • HttpListener.IsSupported is false on XP SP3
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • How can I tell a form not to dispose a particular control when it closes?
  • joining two bezier curves
  • WPF version of .ScaleControl?
  • How to make R's read_csv2() recognise the text characters properly
  • Overlapping controls in Windows XP
  • IE7 and TinyMCE with Plone
  • Implementation of State Monad
  • jQuery ready not fired after rails link_to is clicked
  • Android fill_parent issue
  • QLineEdit password safety
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • How would I use PHP exceptions to define a redirect?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • jquery mobile loadPage not working
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How does Linux kernel interrupt the application?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype