Stretching Bar Snap to Grid

CoffeeMan

New member
Joined
Oct 16, 2008
Messages
4
Programming Experience
3-5
Hi,

I've attached the project I'm working on. The form contains a grid image and a few bars that can be stretched and moved left and right (thanks JohnH!). The bars come form the "Shift.vb" class within the project.

What I'm trying to do is to get the bars to snap to the grid when moved or stretched. I was trying to make the drag surface be a panel, but it was getting a little over my head so I've switched to a grid on an image.

Any suggestions on how to get snap to grid functionality going would be appreciated.

Thanks,
CM
 

Attachments

  • ShiftSnap.zip
    74.4 KB · Views: 32
Last edited by a moderator:
You can for example add this to form code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each c As Control In Me.Controls
        If TypeOf c Is Shift Then
            AddHandler c.MouseUp, AddressOf Shifts_MouseUp
        End If
    Next
End Sub

Private Sub Shifts_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim c As Control = CType(sender, Control)
    Dim loc As Point = c.Location        
    loc.X -= loc.X Mod 25
    c.Location = loc
    c.Width -= c.Width Mod 25
End Sub
Drawing your grid to a multiple of 25 will make it better and easier.
 
Back
Top