Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here's the idea:

I have a big picture box contains a map.. let's called it (MAP) and it has 5 country

and 5 small picture boxes each one has a specific city picture on it.

I wanna drag each one of the city's picture and drop it into the right country position in the map..

and if the user drag it to the correct place in the map.. the city's picture will move to the correct basket, otherwise, to the incorrect basket(I didn't how to do this part so I didn't write anything about it in the code)

I think I should use (x,y), but I couldn't do it :(
please explain to me how can I do it?

the code I'm struggle with:
* pictureBox12 is one of city's picture*
* pictireBox3 is the map*



C#
private void pictureBox12_MouseDown(object sender, MouseEventArgs e)
      {
          CurrentDrag = pictureBox12.AccessibleName;
          PictureBox pictureBoxV = (PictureBox)sender;
          DoDragDrop(pictureBoxV.Image, DragDropEffects.Copy);
      }


C#
private void pictureBox12_MouseMove(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
              pictureBox1.DoDragDrop(random1.ToString(), DragDropEffects.Copy);
          }
      }


C#
private void pictureBox3_DragEnter(object sender, DragEventArgs e)
      {

          e.Effect = DragDropEffects.Move;
          /*if (e.Data.GetDataPresent(DataFormats.Text))
              e.Effect = DragDropEffects.All;
          else
              e.Effect = DragDropEffects.None;
      */}


C#
private void pictureBox3_DragDrop(object sender, DragEventArgs e)
       {

           int x = this.PointToClient(new Point(e.X, e.Y)).X;

           int y = this.PointToClient(new Point(e.X, e.Y)).Y;

           if (x >= pictureBox12.Location.X && x <= pictureBox12.Location.X + pictureBox12.Width && y >= pictureBox12.Location.Y && y <= pictureBox12.Location.Y + pictureBox12.Height)
           {

               string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

               pictureBox1.Image = Image.FromFile(files[0]);

           }

          /* string txt = e.Data.GetData(DataFormats.Text).ToString();
           int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
           pictureBox3.SelectionStart = index;
           pictureBox3.SelectionLength = 0;
           pictureBox3.SelectedText = txt;*/
       }



C#
private void pictureBox3_DragOver(object sender, DragEventArgs e)
      {
        /*  int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
          pictureBox3.SelectionStart = index;
          pictureBox3.SelectionLength = 0;*/
      }
Posted
Comments
BillWoodruff 27-Apr-15 2:32am    
What is the "basket(s)" ?
Member 11643876 27-Apr-15 4:21am    
just a place from the form to hold the correct or incorrect answers
BillWoodruff 27-Apr-15 7:40am    
I'm going to suggest that you can do this in a simpler way without using the "formal" drag-drop facility (implementing 'DragEnter, 'DragOver, 'DragDrop, etc.).

But, if you are using this project to teach yourself about drag-drop, I don't want to distract you. Do you want to see an alternative approach ?

1 solution

You may or may not to use PictureBox. You can use this control, but it has little to do with this problem. Picture box is not a picture, but some (redundant) wrapper control. Instead, you can draw bitmap on bitmap:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Only you need to use the instance of System.Drawing.Graphics used to draw on an image. This is where you get it: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29.aspx[^].

That's all you need to draw an image on image, no matter if this is an image of a PictureBox or not.

Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

On these answers on graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

—SA
 
Share this answer
 
v2

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