Snap to Form's Grid Lines ; Moving Picturebox at Runtime via Mouse Move

  • Thread starter Thread starter Kevin993
  • Start date Start date
K

Kevin993

Guest
Greetings,

I'm using the code below to draw grid lines on my form :


Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Form1.Paint

Dim p As New Pen(Color.Red) With {.DashStyle = Drawing2D.DashStyle.Dot}

Dim x = 0
While x < ClientSize.Width
e.Graphics.DrawLine(p, x, 0, x, ClientSize.Height)
x += 100
End While

Dim y = 0
While y < ClientSize.Height
e.Graphics.DrawLine(p, 0, y, ClientSize.Width, y)
y += 50
End While

End Sub



In previous threads, the case was to create a control (picturebox) at runtime and place it in a corresponding (according to mouse click position on the form) grid line block , as the following :

Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Form1.MouseClick

Dim x = (e.X \ 50) * 100
Dim y = (e.Y \ 50) * 50

Dim picc As New PictureBox With {.Left = x, .Top = y, .Width = 100, .Height = 50}
picc.SizeMode = PictureBoxSizeMode.Normal
picc.BackColor = Color.White
picc.Image = Picturebox1.Image
picc.Location = New Point(e.Location.X - 25, e.Location.Y - 25)
Form1.Controls.Add(picc)

End Sub

I now want to move an already created picturebox on my form and prevent it from moving around the form freely, but to place it to the corresponding grid line block as the mouse moves.

Continue reading...
 
Back
Top