Click here to Skip to main content
12,549,720 members (51,249 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked

How to create selective Drag n Drop handler

, 21 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
 I'm writing this code such that the user of my software can just drag n drop anything he wants and from anywhere. The software will decide what will do with the drag objects.



I'm writing this code such that the user of my software can just drag n drop anything he wants and from anywhere. The software will decide what will do with the drag objects. But my main purpose is to show how easy to create drag n drop operation.


Many software support drag n drop operation. But many of them need user to drag n drop to specified place. 

Using the code

To create drag n drop operation, we just need 3 important things to do:
  1. Allow drop operation in specified control
  2. Write drag enter handler
  3. Write drag drop handler

1. Allow drop operation 

we need to allow our control to accept drop operation. In this case, I make my main form allowed to receive drop objects.

2. Write drag enter handler 

This will enable the program to receive drag objects. For example, in this case, I make copied objects to dropped in my application. You can makes it like cut n paste if you want by specify DragDropEffects.Move instead of DragDropEffects.Copy.
private void frmHashCalculator_DragEnter(object sender, DragEventArgs e)
    e.Effect = DragDropEffects.Copy;

3. Write drop handler

this is the code where your program use the dropped object.
private void frmHashCalculator_DragDrop(object sender, DragEventArgs e)
     if (e.Data.GetDataPresent(DataFormats.FileDrop ,true ))
         string[] lokasi = (string[])e.Data.GetData(DataFormats.FileDrop, true);
         txtLink.Text = lokasi[0];
     else if (e.Data.GetDataPresent(DataFormats.Text ,true ))
         txtComparer.Text = e.Data.GetData(DataFormats.Text, true).ToString();
By checking what the data present type, we can decide what to do with specified data type. In this example, if user drag a file, then the file path will appear to file path section. If user drag a text, then the text will copied to text comparator section.

Points of Interest 

Since my application is used to calculate MD5 hash of file, then I'll show you how easy to calculate MD5 hash.
Cursor = Cursors.WaitCursor;
byte[] retval;
StringBuilder sb;
	FileInfo fi = new FileInfo(txtLink.Text);  //this from file path section
	if (fi.Exists)                             //check if file exist of course
	   file= fi.Open(FileMode.Open, FileAccess.Read);
catch (Exception ex)
if (file != null)
	MD5 md5 = new MD5CryptoServiceProvider();  //just easy as 2 lines of code
	retval = md5.ComputeHash(file);
	file.Close();                              // close the streaam so that the file unlocked
	sb = new StringBuilder();                  //since the result is char array, we can create
	for (int i = 0; i < retval.Length; i++)    //string builder for this
	lblHash.Text = sb.ToString();
Cursor = Cursors.Default ; 
after obtain the result, we can simply comparing the result with text comparator section


Please note that the windows explorer (or maybe other drag source) will hang up until the drop handler is complete. Therefore, for processing a   large file, you must give the process to background thread and complete the drop handler (in UI thread) as soon as possible.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Engineer Donatus Inc.
Indonesia Indonesia
I'm currently a student of Brawijaya University. I work on programming just for hobby. I learn programming since 2nd year of my college. That's when I first bought my PC on year 2004.

You may also be interested in...


Comments and Discussions

GeneralAwesome Man Pin
charles henington29-Mar-11 11:14
membercharles henington29-Mar-11 11:14 
GeneralNice example! Pin
Harold Bamford27-Apr-10 8:58
memberHarold Bamford27-Apr-10 8:58 
GeneralRe: Nice example! Pin
asugix1-May-10 21:16
memberasugix1-May-10 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 22 Mar 2011
Article Copyright 2010 by asugix
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid