Search code examples
c#winformsuser-controlsdraggable

Drag&Drop WinForm UserControl object


I'm trying to d&d UserControl that I created. Here is the UserControl:

enter image description here

For example, I have two UserControls like this in the FlowLayotPanel. When I drag one UserControl to another I want to merge the UserControl data. Merge their labels and images.

According to this article I don't have the ability to pass the draggable UserControl as an argument to DoDragDrop function.

What I'm doing right now is something like this:

        string dataString = "";

        dataString += barcodeLabel.Text + "~";

        string sImg64bit = ImageToBase64(scannedDocImg, ImageFormat.Bmp);

        dataString += sImg64bit;

        DoDragDrop(dataString, DragDropEffects.Copy);   

I'm just passing the data in a string format. I don't think this is good idea? Is there any other way to pass draggable data? Any thoughts?


Solution

  • Thanks to @HansPassant I realized that the answer was under my nose

    Here is what I did:

      private void ScannedDocContainer_MouseDown(object sender, MouseEventArgs e)
      {
         DoDragDrop((ScannedDocContainer)sender, DragDropEffects.Copy);    
      }
    
      private void ScannedDocContainer_DragDrop(object sender, DragEventArgs e)
      {
         if (e.Data.GetDataPresent(typeof(ScannedDocContainer)))
         {
            ScannedDocContainer docContainerDragged = (ScannedDocContainer)e.Data.GetData(typeof(ScannedDocContainer));
            ...
            "Extract the required data here"
            ...
         }
      }
    

    This way I get's the entire dragged UserControl