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

  • Question

  • 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.


    • Edited by Kevin993 Friday, August 2, 2019 5:13 AM
    Friday, August 2, 2019 5:12 AM

Answers

All replies

  • Hi,

    try my code:

    Public Class Form1
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.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
    
        Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
    
            Dim x = (e.X \ 100) * 100
            Dim y = (e.Y \ 50) * 50
            Me.PictureBox1.Location = New Point(x, y)
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.ClientSize = New Size(800, 500)
            PictureBox1.Height = 50
            PictureBox1.Width = 100
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 2, 2019 5:51 AM
  • Hi,

    try my code:

    Public Class Form1
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.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
    
        Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
    
            Dim x = (e.X \ 100) * 100
            Dim y = (e.Y \ 50) * 50
            Me.PictureBox1.Location = New Point(x, y)
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.ClientSize = New Size(800, 500)
            PictureBox1.Height = 50
            PictureBox1.Width = 100
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Yes it works with clicking. But I want to click on a picturebox and while still pressing the left key of mouse and moving around , it should drag the picturebox as the mouse moves (not moving freely wherever the mouse cursor goes, but in the blocks)

    I did this and here is the result :

     Private Sub PictureBox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            If e.Button = MouseButtons.Left Then
    
                Dim x = (e.X \ 100) * 100
                Dim y = (e.Y \ 50) * 50
                PictureBox2.Location = New Point(x, y)
            End If
        End Sub

    as you see, it tries to place the picturebox in many blocks at the same time



    • Edited by Kevin993 Friday, August 2, 2019 6:35 PM
    Friday, August 2, 2019 6:33 PM
  • You can find some threads like : Move a picturebox along a game grid
    • Marked as answer by Kevin993 Friday, August 2, 2019 7:51 PM
    Friday, August 2, 2019 7:02 PM
  • You can find some threads like : Move a picturebox along a game grid
    I didn't find this thread when I searched the net. Thanks for giving the link
    Friday, August 2, 2019 7:51 PM