-2

i want move picturebox in panel but in form have one button. when calling event keyEnter then picturebox in panel can't move? i don't understand? help me?

i want move picturebox!

enter image description here

Gia
  • 81
  • 8

2 Answers2

2

here is the answer to your question using WIN32 API, the picture will move when you click on it and drag it where ever you wanna put it on the form.

    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(pictureBox1.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }
lagripe
  • 766
  • 6
  • 18
0

From the Form1.cs[Design] double click on the button you want to click that moves the ball. This will auto-generate an event. Move the ball by setting the pictureBox.Location to a new Point.

private void button2_Click(object sender, EventArgs e){
        int new_x_location = 12;
        int new_y_location = 34;
        pictureBox1.Location = new Point(new_x_location , new_y_location);

}  

This will move the pictureBox as in teleport it. If you want it to move across the screen as an animation you need to look up linear interpolation starting from here: C# Lerping from position to position