0

I am making a drapandrop and it's working nice. But I want it to give a message whenever I try to drag an image into a picture that already contains one, that won't work... When I click on an image and drag it to the same picturebox where it came from (like I click on picturebox1 and drop it on picturebox1 it just gets blank), it just disappears.public partial class Form1 : Form

    public Form1()
    {
        InitializeComponent();

        pictureBox1.AllowDrop = true;
        pictureBox2.AllowDrop = true;
        pictureBox3.AllowDrop = true;
        pictureBox4.AllowDrop = true;
        pictureBox5.AllowDrop = true;
        pictureBox6.AllowDrop = true;
    }

    void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        DoDragDrop(sender, DragDropEffects.Move);

    }

    void pictureBox_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    void pictureBox_DragDrop(object sender, DragEventArgs e)
    {
        PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;

        if (pb.Image != null) 
        {
            ((PictureBox)sender).Image = pb.Image;
            pb.Image = null;           

        }
        else
        {                
            MessageBox.Show("The picturebox already contains an image.");
        }
     }
}

}

Gurpreet.S
  • 89
  • 1
  • 9
  • Unless I'm reading this wrong, it appears that you're clearing the image by pb.Image=null; – Digital_Utopia May 11 '14 at 13:37
  • Yeah, I want to clear in the picturebox where it came from and move it to the it's destination. – Gurpreet.S May 11 '14 at 13:41
  • Ah, maybe this will help http://stackoverflow.com/questions/16004682/c-sharp-drag-and-drop-from-one-picture-box-into-another – Digital_Utopia May 11 '14 at 13:53
  • Thanks for the link but it's not helping, I have almost everything but I want it not to do dragdrop if there is already an image in the destination picturebox. I thought the if(pb.Image !=null) would work but it's not :( – Gurpreet.S May 11 '14 at 14:41

1 Answers1

0

pb is a reference to the PictureBox that is being dragged. since you are droping on the same PictureBox you are dragging, pb and senrder are both referencing the same PictureBox, so the pb.Image = null; code line is clearing the image in the PictureBox. you need to add to your condition a check that pb is different then sender, like this: if (pb.Image != null && !pb.Equals(sender))

edit

Basically, You should use the DragEnter Event for that. If the PictureBox already contains an Image, you can set the e.Effect to DragDropEffects.None. that will also provide a better solition to the problem you described in the first place.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • thanks that worked for me and how can I stop it dragging if there is already an image in the destination picturebox. – Gurpreet.S May 12 '14 at 14:45
  • @Gurpreet.S See the part of my answer after the **edit** mark – Zohar Peled May 12 '14 at 15:08
  • Do I have to make another picturebox object like in the method DragDrop? PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox; if (pb.Image != null) { e.Effect = DragDropEffects.Move; } else { MessageBox.Show("Already a pic!"); } – Gurpreet.S May 12 '14 at 19:21
  • I wouldn't show a message box in the DragEnter event. it's not very user friendly, and you have to remember that the user might be just passing over the PictureBox with the mouse on it's way to another PictoreBox. it's better to just change the e.Effect to None, this will change the cursor when the user is dragging something over the PictureBox and will not allow the drop anyway. you will, however, have to add a condition on this event hanlder, something like if(((PictureBox)sender).Image != null) {e.Effect = DragDropEffects.None} should solve this problem in an elegant and user friendly way. – Zohar Peled May 13 '14 at 07:04