在面板上移动一个后,pictureBoxes消失了

时间:2014-03-18 09:18:51

标签: c# winforms picturebox

我想将pictureBox中已放置的panel移至panel中的其他位置。问题是,让我说我将3 pictureBox es放入panel并且我想移动其中一个,所选择的一个成功移动但其他的只是消失。我还有List类型PictureBox,其中我存储了pictureBox es,当它们在视觉上消失后,计数保持不变。这是我的代码。

    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));
    }
}
}

截图:
在这里,我放置了3 picturebox es并想要移动带圆圈的那个。 enter image description here 在这里我移动它,其余的消失了。 enter image description here

1 个答案:

答案 0 :(得分:1)

这段代码可能是原因:

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

根据我的理解,此代码将检查所有图片框是否位于points中注册的位置之一。由于所有图片框都位于points中注册的位置之一,因此最后所有三个图片框都将位于draw的同一位置。