Click here to Skip to main content
13,589,266 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


60 bookmarked
Posted 20 Dec 2009
Licenced CPOL

Using the FlowLayoutPanel and Reordering with Drag and Drop

, 20 Dec 2009
Rate this:
Please Sign up or sign in to vote.
Using the FlowLayoutPanel and reordering with drag and drop.


The FlowLayoutPanel control is actually pretty handy from time to time. In this article, I will explain how to enable drag and drop on two controls. I am using a custom progressbar with drag and drop enabled, so this will only show how to drop and reorder controls. As always, there is probably more than one way to solve this, but this is how I did.


Recently, I had to create a custom drawn progressbar and somehow create a list of those with drag and drop functions. After searching the Internet for a while without luck, I decided to write a little helper here once I finished.

Using the Code

To be able to drag controls to the panel, we first have to add the DragEnter event, either by code or in the Designer.

this.flowLayoutPanel1.DragEnter += new DragEventHandler(flowLayoutPanel_DragEnter); 

void flowLayoutPanel_DragEnter(object sender, DragEventArgs e)
    e.Effect = DragDropEffects.All;

Then, we add the actual drop event and write the code for it. First, I check if I have dragged the control to the same FlowLayoutPanel. If that's the case, I reorder; if not, then I first add the control and then reorder. The reordering is based on which child control I "drop" the new control on.

this.flowLayoutPanel1.DragDrop += new DragEventHandler(flowLayoutPanel_DragDrop); 

void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
    HarrProgressBar data = (HarrProgressBar)e.Data.GetData(typeof(HarrProgressBar));
    FlowLayoutPanel _destination = (FlowLayoutPanel)sender;
    FlowLayoutPanel _source = (FlowLayoutPanel)data.Parent;

    if (_source != _destination)
        // Add control to panel
        data.Size = new Size(_destination.Width, 50);
        // Reorder
        Point p = _destination.PointToClient(new Point(e.X, e.Y));
        var item = _destination.GetChildAtPoint(p);
        int index = _destination.Controls.GetChildIndex(item, false);
        _destination.Controls.SetChildIndex(data, index);

        // Invalidate to paint!
        // Just add the control to the new panel.
        // No need to remove from the other panel,
        // this changes the Control.Parent property.
        Point p = _destination.PointToClient(new Point(e.X, e.Y));
        var item = _destination.GetChildAtPoint(p);
        int index = _destination.Controls.GetChildIndex(item, false);
        _destination.Controls.SetChildIndex(data, index);

This will move the control from one panel to another. This code will probably need some tuning if you want to drag from a normal Panel to a FlowLayoutPanel.

Points of Interest

I've put DoDragDrop(); in the HarrProgressBar to be able to drag it onto other controls and to minimize code outside the class.


  • 2009-12-21 - v1.0 - Article added.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Consid
Sweden Sweden
I have been working with .NET development since 2003, working with backend systems, and both web and desktop applications. I have experience of integrating software with ERP-systems, connecting production environment, and publishing information to various platforms. With this comes very extensive knowledge about windows OS administration, both server and client. I have worked with almost every OS from Microsoft, and a few linux dists in mixed environment, along with several mobile and "Thin client"-solutions. I have experience in configuring and managing webservers, ftpservers, domains and mailservers in a mixed OS environment.

I'm always looking forward to learning and mastering new technologies.

Specialties: .NET development, PLC integration, ERP integration
Development -> C#, ASP.NET MVC, HTML, WCF, WinForms, Silverlight, PHP, VB6
Database -> MSSQL, MySQL, MsAccess
ERP -> Jeeves, JSB, Jeeves eSales
Automation integration -> TwinCAT, Siemens S7
Automation HMI -> Iconics, Beijer, Indusoft
Microsoft products -> Sharepoint, Microsoft Office
OS -> Windows Guru (Server & Client), Linux (Debian/Ubuntu)

You may also be interested in...


Comments and Discussions

QuestionHELP! Pin
monroed1130-Oct-16 14:58
membermonroed1130-Oct-16 14:58 
QuestionQuestion Pin
dark-force6-Nov-14 23:05
memberdark-force6-Nov-14 23:05 
QuestionGreat job.. Pin
indesha369-Mar-14 4:47
memberindesha369-Mar-14 4:47 
QuestionCatch mouse double click Pin
takar3-Mar-14 1:57
membertakar3-Mar-14 1:57 
GeneralMy vote of 4 Pin
CodePurveyor23-Aug-13 3:41
memberCodePurveyor23-Aug-13 3:41 
QuestionClarifying where an item is going to drop. Pin
CodePurveyor23-Aug-13 3:31
memberCodePurveyor23-Aug-13 3:31 
GeneralMy vote of 5 Pin
Mazen el Senih13-Aug-13 5:58
memberMazen el Senih13-Aug-13 5:58 
QuestionMy Vote of 5 Pin
Umesh. A Bhat23-Jul-11 0:42
memberUmesh. A Bhat23-Jul-11 0:42 
NewsSome Errors Fixed. Pin
Pouriya.GH4-Feb-11 9:52
memberPouriya.GH4-Feb-11 9:52 
Generalo my god, wonderful Pin
Pouriya.GH2-Feb-11 20:38
memberPouriya.GH2-Feb-11 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180615.1 | Last Updated 21 Dec 2009
Article Copyright 2009 by P.Sandgren
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid