[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4435
  • Last Modified:

FlowLayoutPanel drag/drop reorder

I have several pictureboxes in a FlowLayoutPanel, is there a way to reorder the pictureboxes by dragging and dropping
0
ocsurf
Asked:
ocsurf
1 Solution
 
Bob LearnedCommented:
Working example:

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
            {
                PictureBox picture = new PictureBox();

                picture.AllowDrop = true;
                picture.MouseMove += OnPictureMouseMove;
                picture.DragDrop += OnPictureDragDrop;
                picture.DragEnter += OnPictureDragEnter;
                picture.BorderStyle = BorderStyle.FixedSingle;
                picture.Size = new Size(100, 100);
                picture.BackColor = Color.FromArgb(i * 20, 0, 0);
                this.flowLayoutPanel1.Controls.Add(picture);
            }

            this.flowLayoutPanel1.Height = this.flowLayoutPanel1.Controls[this.flowLayoutPanel1.Controls.Count - 1].Bottom + 5;
        }

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

        private void OnPictureDragDrop(object sender, DragEventArgs e)
        {
            PictureBox target = sender as PictureBox;
            if (target != null)
            {
                int targetIndex = FindPictureBoxIndex(target);
                if (targetIndex != -1)
                {
                    string pictureBoxFormat = typeof(PictureBox).FullName;
                    if (e.Data.GetDataPresent(pictureBoxFormat))
                    {
                        PictureBox source = e.Data.GetData(pictureBoxFormat) as PictureBox;

                        int sourceIndex = this.FindPictureBoxIndex(source);

                        if (targetIndex != -1)
                            this.flowLayoutPanel1.Controls.SetChildIndex(source, targetIndex);
                    }
                }
            }
        }

        private int FindPictureBoxIndex(PictureBox picture)
        {
            for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
            {
                PictureBox target = this.flowLayoutPanel1.Controls[i] as PictureBox;

                if (picture == target)
                    return i;
            }
            return -1;
        }

        private void OnPictureMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox picture = sender as PictureBox;

                picture.BorderStyle = BorderStyle.Fixed3D;
                picture.DoDragDrop(picture, DragDropEffects.Move);

            }
        }

Bob
1

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now