Search code examples
c#drag-and-dropflowlayoutpanel

dragging user controls from one flowlayoutpanel to another


how can i add drag and drop capability of a custom usercontrol from 2 flowlayoutpanels?

Usercontrol keeps being null on the following line of code below

    private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
    {
        UserControl userControl = e.Data.GetData(typeof(UserControl)) as UserControl;

Solution

  • The problem with what you are doing is that to extract the data that is stored inside the drag, you have to specify the exact type.

    control.DoDragDrop(new Label(), DragDropEffects.Move);
    
    e.Data.GetDataPresent(typeof(Control)) // = false
    e.Data.GetDataPresent(typeof(Label)) // = true
    

    What you have to do is create a wrapper and use that for your drag-and-drop code.

    class ControlWrapper
    {
      public Control Control { get; private set; }
      public ControlWrapper(Control control) { Control = control; }
    }
    
    control.DoDragDrop(new ControlWrapper(new Label()), DragDropEffects.Move);
    
    e.Data.GetDataPresent(typeof(ControlWrapper)) // = true
    

    And your code now becomes

    ControlWrapper controlWrapper = e.Data.GetData(typeof(ControlWrapper)) as ControlWrapper;
    UserControl userControl = controlWrapper.Control as UserControl;
    

    And trying not to sound too preachy, but you should check if the data exists in the form you want first. e.Data.GetDataPresent(typeof(ControlWrapper)) will tell you if it is that format.