Anda di halaman 1dari 1

Moving a control by dragging it with the mouse in C#

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}

private Point MouseDownLocation;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)


{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)


{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Left = e.X + pictureBox1.Left - MouseDownLocation.X;
pictureBox1.Top = e.Y + pictureBox1.Top - MouseDownLocation.Y;
}
}

Anda mungkin juga menyukai