Drag & drop

 Posted by  Programming  Add comments  Tagged with:
Dec 152011
 

It is sad how often I see applications that could have use of drag&drop mechanism but are blissfully unaware of it. Sad part is that half of those annoying applications are mine. :)

And it is annoyingly easy to implement. In addition to setting AllowDrop=true on your favorite control or form you need to implement two events:

private void list_DragEnter(object sender, DragEventArgs e) {
    e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Move : DragDropEffects.None;
}

private void list_DragDrop(object sender, DragEventArgs e) {
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
        var files = (string[])e.Data.GetData(DataFormats.FileDrop);
        DoSomething(files);
    }
}

And this is all there is to it.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>