Question

I want to move an already placed pictureBox in a panel to another location in the panel. The thing is, let's say I place 3 pictureBoxes into the panel and I want to move one of them, the selected one moves successfully but the others just disappear. I also have a List of type PictureBox, in which I store the pictureBoxes, and the count stays the same of course, after they disappear visually. Here is my code.

    namespace DragDrop
{
public partial class Form1 : Form
{
    List<Point> points;
    List<PictureBox> pics;

    public Form1()
    {
        InitializeComponent();
        InitializePoints();
        pics = new List<PictureBox>();
    }

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

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

    private void panel1_DragDrop(object sender, DragEventArgs e)
    {
        Point cursor = PointToClient(Cursor.Position);
        Point draw = new Point();

        foreach (Point p in points)
        {
            if (cursor.X > p.X && cursor.Y > p.Y)
            {
                draw = p;
            }
        }

        if (e.AllowedEffect == DragDropEffects.Move)
        {
            foreach (Point p in points)
            {
                foreach (PictureBox pbb in pics)
                {
                    if (pbb.Location == p)
                    {
                        pbb.Parent = this;
                        pbb.Location = draw;
                        Console.WriteLine(pics.Count());
                    }
                }
            }
        }
        else
        {
            PictureBox pb = new PictureBox();
            pb.Height = panel1.Height / 3;
            pb.Width = panel1.Width / 4;
            pb.SizeMode = PictureBoxSizeMode.StretchImage;
            pb.MouseDown += new MouseEventHandler(Mouse_Down);
            pb.Parent = this;
            pics.Add(pb);
            this.Controls.Add(pb);

            pb.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
            pb.BringToFront();
            pb.Location = draw;
        }
    }

    private void Mouse_Down(object sender, EventArgs e)
    {
        ((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Move);
    }

    private void InitializePoints()
    {
        points = new List<Point>();

        int pleft = panel1.Left;
        int ptop = panel1.Top;
        int cellwidth = panel1.Width / 4;
        int cellheigth = panel1.Height / 3;

        //First row
        points.Add(new Point(pleft, ptop));
        points.Add(new Point(pleft + cellwidth, ptop));
        points.Add(new Point(pleft + 2 * cellwidth, ptop));
        points.Add(new Point(pleft + 3 * cellwidth, ptop));

        //Second row
        points.Add(new Point(pleft, ptop + cellheigth));
        points.Add(new Point(pleft + cellwidth, ptop + cellheigth));
        points.Add(new Point(pleft + 2 * cellwidth, ptop + cellheigth));
        points.Add(new Point(pleft + 3 * cellwidth, ptop + cellheigth));

        //Third row
        points.Add(new Point(pleft, ptop + 2 * cellheigth));
        points.Add(new Point(pleft + cellwidth, ptop + 2 * cellheigth));
        points.Add(new Point(pleft + 2 * cellwidth, ptop + 2 * cellheigth));
        points.Add(new Point(pleft + 3 * cellwidth, ptop + 2 * cellheigth));
    }
}
}

Screenshots:
Here I have placed 3 pictureboxes and want to move the circled one. enter image description here And here I moved it and the rest disappeared. enter image description here

Was it helpful?

Solution

This piece of code might be the cause:

foreach (Point p in points)
{
    foreach (PictureBox pbb in pics)
    {
        if (pbb.Location == p)
        {
            pbb.Parent = this;
            pbb.Location = draw;
            Console.WriteLine(pics.Count());
        }
    }
}

From my understanding, this code will check through all picture box whether it is located in one of the location registered in points. Since all picture box is located at one of the location registered in points, in the end all three picture box will be located at the same location which is draw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top