Preventing overlapping objects

Sometimes people ask how to prevent users from dragging rectangular objects so that they overlap.

You just need to implement GoView.BackgroundSelectionDropReject and GoView.ObjectSelectionDropReject event handlers. Something like:

[code] // reject drops when there would still be overlap with some non-link selectable document object
private void goView1_BackgroundSelectionDropReject(object sender, Northwoods.Go.GoInputEventArgs e) {
// the user may be dragging multiple selected objects, so we need to check each one for any overlaps
foreach (GoObject obj in goView1.Selection) {
// find all selectable objects within the obj.Bounds
IGoCollection coll = goView1.PickObjectsInRectangle(true, false, obj.Bounds,
GoPickInRectangleStyle.SelectableOnlyIntersectsBounds, null, 999999);
foreach (GoObject x in coll) {
// ignore links and other selected objects
if (x is IGoLink) continue;
if (goView1.Selection.Contains(x)) continue;
// OK if object’s Bounds do not intersect with selected object Bounds
if (!x.Bounds.IntersectsWith(obj.Bounds)) continue;
// otherwise, reject the drop
e.InputState = GoInputState.Cancel;
break;
}
// if we’ve already found one node overlap, don’t bother looking for more
if (e.InputState == GoInputState.Cancel) break;
}
}

private void goView1_ObjectSelectionDropReject(object sender, Northwoods.Go.GoObjectEventArgs e) {
  goView1_BackgroundSelectionDropReject(sender, e);
}[/code]

If you have distinctly non-rectangular or hollow objects where you may want to allow a drop, because they don’t really intersect even though their Bounds do, you may want to make the RectangleF.IntersectsWith test smarter.

Also, a related topic: avoiding overlapping objects when resizing: http://www.nwoods.com/forum/forum_posts.asp?TID=1524