FlowLayoutPanel drag/drop reorder

Posted on 2007-10-18
Last Modified: 2008-01-09
I have several pictureboxes in a FlowLayoutPanel, is there a way to reorder the pictureboxes by dragging and dropping
Question by:ocsurf
    1 Comment
    LVL 96

    Accepted Solution

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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now