yaron89
asked on
User control of picturebox location
What is the best way to enable the user changing the location of picturebox or buttons on a form?
do you mean changing the location of the image in a picture box, e.g. specifying the image displayed is c:\temp\pic1.gif? or do you mean, the picture box should now be at a new location, e.g. 200 pixels from the top and 300 pixels from the left on the form?
ASKER
I mean dragging it to a new location on the form.
You can use the mouse up/down.move events like below
private bool isDragging = false;
private int currentX, currentY;
private void Form1_Load(object sender, EventArgs e)
{
// load from some config file if needed for last set position
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
currentX = e.X;
currentY = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.pictureBox1.Top = this.pictureBox1.Top + (e.Y - currentY);
this.pictureBox1.Left = this.pictureBox1.Left + (e.X - currentX);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
// save the current values to some config file if needed
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You