Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to create selective Drag n Drop handler

, 21 Mar 2011
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.

Introduction

screen_shoot.png
 
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.
 

Background

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 

allow_drop.png
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;
try
{
	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)
{
	MessageBox.Show(ex.Message);
	//throw;
}
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
	{
		sb.Append(retval[i].ToString("X2"));
	}
	lblHash.Text = sb.ToString();
}
Cursor = Cursors.Default ; 
after obtain the result, we can simply comparing the result with text comparator section
 

Note

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.

License

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

Share

About the Author

asugix
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.

Comments and Discussions

 
GeneralAwesome Man Pinmembercharles henington29-Mar-11 11:14 
GeneralNice example! PinmemberHarold Bamford27-Apr-10 8:58 
GeneralRe: Nice example! Pinmemberasugix1-May-10 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 22 Mar 2011
Article Copyright 2010 by asugix
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid