Drag and drop user controls from one flowlayoutpanel to another

How can I add the ability to drag and drop a user control from 2 stream packets?

Usercontrol stores null in the next line of code below

    private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
    {
        UserControl userControl = e.Data.GetData(typeof(UserControl)) as UserControl;
+3
source share
1 answer

The problem with what you are doing is that in order to retrieve the data that is stored inside the drag and drop, you must specify the exact type .

control.DoDragDrop(new Label(), DragDropEffects.Move);

e.Data.GetDataPresent(typeof(Control)) // = false
e.Data.GetDataPresent(typeof(Label)) // = true

What you need to do is create a wrapper and use it 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 now your code becomes

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

, , , . e.Data.GetDataPresent(typeof(ControlWrapper)) , .

+3

Source: https://habr.com/ru/post/1706699/


All Articles