Imagine a form with lots of randomly positioned colored labels on.
The arrow keys allow the user to move the 'selected' label in a given direction such that each press of a button 'snaps' the selected label to the vertices of the next nearest label.
I started this by building a 2 lists of the label vertices....
lstX.Clear()
lstY.Clear()
For Each Shape In Main.Shapes
If Not lstX.Contains(Shape.Locati
on.X) Then
lstX.Add(Shape.Location.X)
End If
If Not lstX.Contains(Shape.Locati
on.X + Shape.Size.Width) Then
lstX.Add(Shape.Location.X + Shape.Size.Width)
End If
If Not lstY.Contains(Shape.Locati
on.Y) Then
lstY.Add(Shape.Location.Y)
End If
If Not lstY.Contains(Shape.Locati
on.Y + Shape.Size.Height) Then
lstY.Add(Shape.Location.Y + Shape.Size.Height)
End If
Next
lstX.Sort()
lstY.Sort()
which is refreshed every time a label moves.
On key press i fetch the postion of the currentshape in the list and return the next vertex
Case Keys.Up
currentShape.Location = New Point(currentShape.Locatio
n.X, lstY.ElementAt(lstY.FindIn
dex(Functi
on(iFinder
As Integer) iFinder = currentShape.Location.Y) - 1))
Case Keys.Down
currentShape.Location = New Point(currentShape.Locatio
n.X, lstY.ElementAt(lstY.FindIn
dex(Functi
on(iFinder
As Integer) iFinder = currentShape.Location.Y) + 1))
Case Keys.Left
currentShape.Location = New Point(lstX.ElementAt(lstX.
FindIndex(
Function(i
Finder As Integer) iFinder = currentShape.Location.X) - 1), currentShape.Location.Y)
Case Keys.Right
currentShape.Location = New Point(lstX.ElementAt(lstX.
FindIndex(
Function(i
Finder As Integer) iFinder = currentShape.Location.X + currentShape.Width) + 1), currentShape.Location.Y)
Up and Left work as they should but Down and Right do not. I need to factor in the height/width of the selected shape and prevent it from snapping to it's own bottom/right vertex.
Could be going about this completely the wrong way !
Ideas please ?
Start Free Trial