SoFunction
Updated on 2025-03-07

Quickly implement drag and drop operations in C#

Drag and drop operation is a user experience I prefer, but it is a bit troublesome to implement. Here I have simply collected its common methods as an extension method to quickly call:

static class DrapDropExtend
{
    public static void SimpleDrapDrop<T>(this Control c, string dataformat, Action<T> hanlder) where T : class
    {
         = true;
         += (s, e) =>
            {
                if ((dataformat))
                     = ;
                else
                     = ;
            };

         += (s, e) =>
            {
                var data = (dataformat) as T;
                hanlder(data);
            };
    }

    public static void SimpleDrapDrop(this Control c, Action<DragEventArgs> enterHanlder, Action<DragEventArgs> dropHanlder)
    {
         = true;
         += (s, e) => enterHanlder(e);
         += (s, e) => enterHanlder(e);
    }

    public static void SimpleDrapDrop(this Control c, DragEventHandler enterHanlder, DragEventHandler dropHanlder)
    {
         = true;
         += enterHanlder;
         += dropHanlder;
    }
}

This class makes it easier to implement drag and drop. A simple example is as follows:

public Form1()
{
    InitializeComponent();
    <string>(, x =>  = x);
}

This is much simpler than implementing it through an IDE.

This is all about this article about C#’s quick drag and drop operation. I hope it will be helpful to everyone's learning and I hope everyone will support me more.