Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am making a simple 2D map maker for games. Simply the user clicks on a button that will show a openfiledialog. The user chooses a file and is loaded in a PictureBox. Then if the user wants to add it to the game world(panel) he/she clicks on the add button and the panel will load the picture. What I am having trouble with is being able to drag and drop the image within that panel. I will appreciate your help.

windows 7 visual C# winform

thanks,
Posted
Updated 15-Feb-12 11:42am
v3

Hello,

I think this can help:

C#
private bool isPictureReadyToDrag;
private void SetPosition()
{
     MyPictureBox.Location = new Point(MousePosition.X - this.Left - MyPictureBox.Width / 2, MousePosition.Y - this.Top - MyPictureBox.Height);
}

private void MyPictureBox_MouseMove(object sender, MouseEventArgs e)
{
     if (isPictureReadyToDrag)
         SetPosition();
}

private void MyPictureBox_MouseUp(object sender, MouseEventArgs e)
{
     isPictureReadyToDrag = false;
}

private void MyPictureBox_MouseDown(object sender, MouseEventArgs e)
{
     isPictureReadyToDrag = true;
     SetPosition();
}
 
Share this answer
 
Comments
MR. AngelMendez 15-Feb-12 18:18pm    
Both of your solutions were helpful, Great examples from both of you.

Thanks,
Shahin Khorshidnia 15-Feb-12 18:36pm    
Thank you.
[no name] 15-Feb-12 20:09pm    
any time...cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900