I am trying to make an app to generate a tierlist.
Each tier consists of a flowlayout and you basically move the controls around by drag-and-drop.
The problem is that even though I can move the elements with drag-and-drop, the element is always added to the end of the list, and not where you drop the pointer.
Is there any way to maintain order during drag-and-drop?
These are the methods that are in the flowlayout
private void flow_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void flow_DragDrop(object sender, DragEventArgs e)
{
((UserControl1)e.Data.GetData(typeof(UserControl1))).Parent = (Panel)sender;
}
While these are from the usercontrol:
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
this.DoDragDrop(this, DragDropEffects.Move);
}
You can opt this solution to reorder the dropped controls into a
FlowLayoutPanelcontrol. The part that utilizes theControl.ControlCollection.GetChildIndexandControl.ControlCollection.SetChildIndexmethods.Let's say you have a custom Control or UserControl named
DragDropControl:Note: From what I see in the images, just use a simple
Labelcontrol instead.Let's create a custom
FlowLayoutPaneland encapsulate all the required functionalities to not repeat that in your implementation for eachFLP.The custom
FLPimplements the mouse, drag and drop events of its children as well.Handle the child controls
MouseDownevent to do the drag:Handle the
DragEnterandDragOvermethods and events of theFLPand its children to set the drop effect.Finally, call from the
DragDropmethod override and event theDropControlmethod and pass theDragEventArgsparam.The dropped control takes the index of the control under the mouse position if any otherwise it will be inserted at the end of the
Controlscollection.Put it all together.