Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime
Snap Controls to eachother at runtime Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Go Back  Xtreme Visual Basic Talk > > > Snap Controls to eachother at runtime


Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2011, 10:42 AM
das18 das18 is offline
Newcomer
 
Join Date: Aug 2010
Posts: 3
Question Snap Controls to eachother at runtime


I have so far written a program where you click a button and a custom control is added to the form. These controls can then be dragged and positioned anywhere on the form.

I know want to be able to align/snap these controls.

Please advise if you know of a way to drag a control on a form and snap it in line with another control?

Thanks
Reply With Quote
  #2  
Old 09-21-2011, 11:51 AM
AtmaWeapon's Avatar
AtmaWeaponSnap Controls to eachother at runtime AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It's easiest to find a snapping/docking framework that does it for you.

If you want to do it yourself, then your drag code has to ask all of the other controls, "Am I close to you?" When the control is dragged close to another control, you snap by figuring out what side you're on then setting appropriate locations.

Let's say you've decided "close" means "within 10 pixels of an edge". Suppose we're dragging Button2 around. A "Should I snap?" algorithm might look like this:
Code:
For each other control:
    Is my left edge close to this control's right edge?
        If so snap to that control's right edge.
    Is my top edge close to this control's bottom edge?
        If so snap to that control's bottom edge.
    ...
How do you tell if one edge is close to another? Compare coordinates. Here's checking left vs. right, assuming SnapThreshold is a constant set to how close is "close":
Code:
Dim distance = Math.Abs(Me.Left - other.Right)
If distance <= SnapThreshold
    <snap my left edge to the control's right edge>
End If
The algorithm to snap is pretty simple: set whatever edge is snapping to the same as the value for the edge it is snapping to. Here's what snapping left to right from the last example might look like:
Code:
Me.Left = other.Right
Obviously there's a lot of tedium involved. If you're clever you can reduce some of it by using functions, but there's still a good bit of stuff you can't collapse. You'll also have to be clever about what to do when there's many controls and multiple snap candidates; I can easily imagine a scenario where this naive algorithm would get you stuck in an infinite fight between two controls. My code also doesn't allow you to "break" the snap. Seems like you might want to keep track of which controls are considered paired; that'd make addressing those problems easier.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
 
Snap Controls to eachother at runtime
Snap Controls to eachother at runtime
 
-->