how do I drag two different images from two different pictureboxes to two different pictureboxes?

Viewed 44

I'm going to create a game. picturebox1 and picturebox4 have different images, picturebox2 and picturebox3 have no image. I want two different movement operations. i want to move the image from picturebox1 to picturebox2 and the Image from picturebox4 to picturebox3. But in the code I wrote, if I drag the first picturebox1, I can drop it to picturebox2 as I want, but when I select picturebox4, the second operation, I can't drop it to picturebox3 and it allows me to drop it to picturebox2. Or if I drag the first picturebox4, I can drop it to picturebox3 as I want, but when I select the second process, picturebox1, I can't drop it to picturebox2, and it allows me to drop it to picturebox3. How can I solve this problem in visual studio form application ?

enter code here    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace picturebox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox2.AllowDrop = true;
            pictureBox3.AllowDrop = true;
            label1.Visible = false;
            label2.Visible = false;
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
        }
        private void pictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            label1.Visible = false;
            Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox2.Image = getPicture;
        }
        private void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
                label1.Visible = true;
                pictureBox3.AllowDrop = false;
                if (e.Data.GetDataPresent(DataFormats.Bitmap))
                {
                    e.Effect = DragDropEffects.Copy;
                }  
        }

        private void pictureBox2_DragLeave(object sender, EventArgs e)
        {
            label1.Visible = false;
        
        }

    

        private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);          
        }



         private void pictureBox3_DragDrop(object sender, DragEventArgs e)
         {
            label2.Visible = false;
            label1.Visible = true;
            Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
            pictureBox3.Image = getPicture;
        } 

         private void pictureBox3_DragEnter(object sender, DragEventArgs e)
         {
                label2.Visible = true;
                pictureBox2.AllowDrop = false;
                if (e.Data.GetDataPresent(DataFormats.Bitmap))
                {
                    e.Effect = DragDropEffects.Copy;

                }
      
         }

         private void pictureBox3_DragLeave(object sender, EventArgs e)
         {
            label2.Visible = false;
         }
     }
}
1 Answers

Following Hans's tip, I modified the code for you.

Only do the simplest display, modify it according to your needs.

Pass the PictureBox to DoDragDrop():

 ((PictureBox)sender).DoDragDrop(((PictureBox)sender), DragDropEffects.Copy);

Check the Name property in the DragEnter:

 if (getPicture.Name != pictureBox3/4.Name) return;

Code:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox2.AllowDrop = true;
            pictureBox3.AllowDrop = true;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender), DragDropEffects.Copy);
        }

        private void pictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(PictureBox))) return;
            PictureBox getPicture = (PictureBox)e.Data.GetData(typeof(PictureBox));
            if (getPicture.Name != pictureBox1.Name) return;
            e.Effect = DragDropEffects.Copy;
            pictureBox2.Image = getPicture.Image;
        }

        private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
        {
            ((PictureBox)sender).DoDragDrop(((PictureBox)sender), DragDropEffects.Copy);
        }

        private void pictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(PictureBox))) return;
            PictureBox getPicture = (PictureBox)e.Data.GetData(typeof(PictureBox));
            if (getPicture.Name != pictureBox4.Name) return;
            e.Effect = DragDropEffects.Copy;
            pictureBox3.Image = getPicture.Image;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Process process = Process.GetCurrentProcess();
            process.Close();
            Application.Restart();
        }
    }
}

Design:

enter image description here

Output:

enter image description here

Related