I want to be able to drag and drop an image into any picture box in my array, however when I drag and drop my image into a picture box in that array, the picture box drags into all picture boxes in that array. How do I solve this? Here's my code:
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);
}
}
}
}
picBananaOG is the picture I am dragging. The array name is Place1. I only want the picture to drag to the picture I am dragging it to, but it keeps dragging to every picture in the array. I am only doing this for the Place1 array, so ignore the others.