C# 如何将图片拖放到阵列中的特定图片框中?(重新询问,删除原件)
我希望能够将图像拖放到数组中的任何图片框中,但是,当我将图像拖放到该数组中的图片框中时,图片框将拖放到该数组中的所有图片框中。我如何解决这个问题?这是我的密码:C# 如何将图片拖放到阵列中的特定图片框中?(重新询问,删除原件),c#,arrays,drag-and-drop,visual-studio-2017,picturebox,C#,Arrays,Drag And Drop,Visual Studio 2017,Picturebox,我希望能够将图像拖放到数组中的任何图片框中,但是,当我将图像拖放到该数组中的图片框中时,图片框将拖放到该数组中的所有图片框中。我如何解决这个问题?这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.
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 finalProject
{
public partial class Form1 : Form
{
PictureBox[] Place1 = new PictureBox[8];
PictureBox[] Place2 = new PictureBox[8];
PictureBox[] Place3 = new PictureBox[8];
PictureBox[] Place4 = new PictureBox[8];
PictureBox[] Place5 = new PictureBox[8];
System.Random r = new System.Random((int)System.DateTime.Now.Ticks);
public Form1()
{
InitializeComponent();
picBananaTreeOG.MouseDown += picBananaTreeOG_MouseDown;
}
private void Form1_Load(object sender, EventArgs e)
{
//assigning array to planting spots
Place1[0] = place1;
Place1[1] = place2;
Place1[2] = place3;
Place1[3] = place4;
Place1[4] = place5;
Place1[5] = place6;
Place1[6] = place7;
Place1[7] = place8;
Place2[0] = place9;
Place2[1] = place10;
Place2[2] = place11;
Place2[3] = place12;
Place2[4] = place13;
Place2[5] = place14;
Place2[6] = place15;
Place2[7] = place16;
Place3[0] = place17;
Place3[1] = place18;
Place3[2] = place19;
Place3[3] = place18;
Place3[4] = place19;
Place3[5] = place20;
Place3[6] = place21;
Place3[7] = place22;
Place4[0] = place23;
Place4[1] = place24;
Place4[2] = place25;
Place4[3] = place26;
Place4[4] = place27;
Place4[5] = place28;
Place4[6] = place29;
Place4[7] = place30;
Place5[0] = place31;
Place5[1] = place32;
Place5[2] = place33;
Place5[3] = place34;
Place5[4] = place35;
Place5[5] = place36;
Place5[6] = place37;
Place5[7] = place38;
for (int i = 0; i < Place1.Length; i++)
{
Place1[i].AllowDrop = true;
Place1[i].DragEnter += Place1_DragEnter;
Place1[i].DragDrop += Place1_DragDrop;
}
}
private void picBananaTreeOG_MouseDown(object sender, MouseEventArgs e)
{
var img = picBananaTreeOG.Image;
if (img == null) return;
if (e.Button == MouseButtons.Left)
{
picBananaTreeOG.DoDragDrop(picBananaTreeOG.Image,
DragDropEffects.Copy);
}
picBananaTreeOG.Image = picBananaTreeOG.Image;
}
private void Place1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Place1_DragDrop(object sender, DragEventArgs e)
{
for (int i = 0; i < Place1.Length; i++)
{
Place1[i].Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间最终项目
{
公共部分类Form1:Form
{
PictureBox[]Place1=新的PictureBox[8];
PictureBox[]Place2=新的PictureBox[8];
PictureBox[]Place3=新PictureBox[8];
PictureBox[]Place4=新PictureBox[8];
PictureBox[]Place5=新PictureBox[8];
System.Random r=new System.Random((int)System.DateTime.Now.Ticks);
公共表格1()
{
初始化组件();
picbananatreog.MouseDown+=picbananatreog_MouseDown;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//为种植点分配阵列
Place1[0]=Place1;
地点1[1]=地点2;
地点1[2]=地点3;
地点1[3]=地点4;
地点1[4]=地点5;
地点1[5]=地点6;
地点1[6]=地点7;
地点1[7]=地点8;
地点2[0]=地点9;
地点2[1]=地点10;
地点2[2]=地点11;
地点2[3]=地点12;
地点2[4]=地点13;
地点2[5]=地点14;
地点2[6]=地点15;
地点2[7]=地点16;
地点3[0]=地点17;
地点3[1]=地点18;
地点3[2]=地点19;
地点3[3]=地点18;
地点3[4]=地点19;
地点3[5]=地点20;
地点3[6]=地点21;
地点3[7]=地点22;
地点4[0]=地点23;
地点4[1]=地点24;
地点4[2]=地点25;
地点4[3]=地点26;
地点4[4]=地点27;
地点4[5]=地点28;
地点4[6]=地点29;
地点4[7]=地点30;
地点5[0]=地点31;
地点5[1]=地点32;
地点5[2]=地点33;
地点5[3]=地点34;
地点5[4]=地点35;
地点5[5]=地点36;
地点5[6]=地点37;
地点5[7]=地点38;
for(int i=0;i
picBananaOG是我正在拖动的图片。数组名为Place1。我只想将图片拖动到我要将其拖动到的图片,但它会一直拖动到数组中的每个图片。我只对Place1数组执行此操作,因此忽略其他数组。将其添加到每个pbox。将发送者强制转换到picturebox并仅将其放到那里:
((picturebox(发送者)).Image=…
@TaW我应该将其放在代码中的什么位置?在您的公共位置1_DragDrop事件中,替换loop@TaW我不是很有经验,所以我对你的意思和省略号应该代表什么感到困惑。当然,你的作业代码的其余部分:=(位图)e.Data.GetData(DataFormats.Bitmap,true)
假设它正在工作。。