Drag and drop within a FlowLayoutPanel

I needed to be able to use drag and drop within a FlowLayoutPanel for the rent program I am currently working on. Was a hard nut to crack (basicly beacuse I was stupid and there where slim to none information on how to do exactly this). Further down there are a rough (need to be polished with more test etc) demo code that shows how to do it.

To use it make a UserControl with a background color. Copy and paste this:

public partial class ucFlowLayoutDraggable : UserControl
public ucFlowLayoutDraggable()
AllowDrop = true;

protected override void OnMouseDown(MouseEventArgs e)
DoDragDrop(this, DragDropEffects.All);

protected override void OnDragOver(DragEventArgs drgevent)
// is another dragable
if (drgevent.Data.GetData(typeof(ucFlowLayoutDraggable))!=null)
FlowLayoutPanel p = (FlowLayoutPanel)Parent;
int myIndex = p.Controls.GetChildIndex(this);
typeof(ucFlowLayoutDraggable)), myIndex);

On your form add a FlowLayoutPanel and set the AllowDrop property to true, add a couple of usercontrols to it (resize them to different sizes so that you can see what happens).

Really easy when you see the code 🙂

2 Responses to “Drag and drop within a FlowLayoutPanel”

  1. Charles Robinson

    I have a usercontrol that contains multiple other controls, and this isn’t working. The MouseDown is being triggered on the individual controls inside the usercontrol and I can’t figure out how to make this percolate events up to the usercontrol. Any ideas?

  2. Mikael Ohlsson

    Oh.. was a while ago since I did this and I forgot how I finally managed to do it (I went with another solution in the end.. so there is no code taht I can give you). The only thing I remember is that I had to do everything in very small steps and debug. It was quite messy…

    Good luck, I hope you solve it. If you do please post a comment how you did it.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>