简体   繁体   English

将图像从 PictureBox 拖放到另一个 PictureBox 并检查目标 PictureBox 是否正确

[英]Drag and drop image from a PictureBox to another PictureBox and check if is the target PictureBox is the correct one

I'm new to programming.我是编程新手。 I'm making an application for first-grade kids in Visual Studio 2019 WindowsForm and one level is about dragging a picture from a PictureBox in an empty PictureBox that has a label with a simple definition.我正在为 Visual Studio 2019 WindowsForm中的一年级孩子制作一个应用程序,其中一个级别是关于从具有简单定义的 label 的空PictureBox中的PictureBox拖动图片。

So if I have 2 PictureBox that show let say a dog and a chicken and 2 empty PictureBox (1 with label "bones" and one with label "grain").因此,如果我有 2 个显示狗和鸡的PictureBox和 2 个空的PictureBox (1 个带有 label “骨头”,一个带有 label “谷物”)。 I want to drag the dog picture into the PictureBox with the label "bones" and the chicken in the one with "grain" label and if correct I show a text "great job" and if not I show a text "try again".我想将带有 label“骨头”的狗图片拖到PictureBox中,将鸡肉拖入带有“谷物”label 的图片框中,如果正确,我会显示文字“干得好”,如果不是,我会显示文字“再试一次”。

I can drag the pictures but I can't find a method to check if is correct.我可以拖动图片,但找不到检查是否正确的方法。 Can anybody help me with that?有人可以帮我吗?

Here is my code so far:到目前为止,这是我的代码:

    private void NivelulDoi_Load(object sender, EventArgs e)
        {
            customPictureBox1.AllowDrop = true;
            customPictureBox2.AllowDrop = true;
            customPictureBox3.AllowDrop = true;
        }

        private void customPictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                customPictureBox1.DoDragDrop(customPictureBox1.Image, DragDropEffects.Copy);
            }
        }

        private void customPictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            var data = e.Data.GetData(DataFormats.FileDrop);
            if (data != null)
            {
                var fileNames = data as string[];
                if (fileNames.Length > 0)
                {
                    customPictureBox1.Image = Image.FromFile(fileNames[0]);
                }
            }
        }

        private void customPictureBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void customPictureBox2_DragEnter(object sender, DragEventArgs e)
        {
            Dragging(e);
        }

        private void customPictureBox2_DragDrop(object sender, DragEventArgs e)
        {
            customPictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
            
            // figure out how to check if correct 
            /*if (customPictureBox2.Image == (Bitmap)e.Data.GetData(DataFormats.Bitmap, true))
            {
                label1.Text = "ai reusit";
            }
            else
            {
                label1.Text = "mai incearca";
            }*/   
        }

        private void customPictureBox3_DragEnter(object sender, DragEventArgs e)
        {
            Dragging(e);
        }

        private void customPictureBox3_DragDrop(object sender, DragEventArgs e)
        {
            customPictureBox3.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
        }

        private void customPictureBox4_DragEnter(object sender, DragEventArgs e)
        {
            Dragging(e);
        }

        private void customPictureBox4_DragDrop(object sender, DragEventArgs e)
        {
            customPictureBox4.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
        }

        private static void Dragging(DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0)
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
    }
}

Thank you.谢谢你。

A simple way is to define a variable to save the Name of "picturebox drag" and a dictionary to save "drag-drop" pair.一个简单的方法是定义一个变量来保存“图片框拖动”的名称和一个字典来保存“拖放”对。

Here is the demo.这是演示。

string pictureBoxFrom = "";
Dictionary<string, string> picDict = new Dictionary<string, string>();
public Form1()
{
    InitializeComponent();
    pictureBox2.AllowDrop = true;
    pictureBox4.AllowDrop = true;
    pictureBox1.MouseDown += pictureBox_MouseDown;
    pictureBox3.MouseDown += pictureBox_MouseDown;
    pictureBox2.DragEnter += pictureBox_DragEnter;
    pictureBox2.DragDrop += pictureBox_DragDrop;
    pictureBox4.DragEnter += pictureBox_DragEnter;
    pictureBox4.DragDrop += pictureBox_DragDrop;

    picDict.Add("pictureBox1", "pictureBox2"); // drag 1 to 2
    picDict.Add("pictureBox3", "pictureBox4"); // drag 3 to 4
}

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
    pictureBoxFrom = (sender as PictureBox).Name;
    var img = (sender as PictureBox).Image;
    if (img == null) return;
    if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
    {
        //(sender as PictureBox).Image = null;
    }
}

private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.Bitmap))
        e.Effect = DragDropEffects.Move;
}

private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
    if (picDict.ContainsKey(pictureBoxFrom) && picDict[pictureBoxFrom].Equals((sender as PictureBox).Name))
    {
        var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
        (sender as PictureBox).Image = bmp;
        MessageBox.Show("great job");
    }
    else
    {
        MessageBox.Show("try again");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM