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

Modifying DataObject During Drag and Drop Operation

, 14 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A demo on how to handle and modify the data object on a drop event

Introduction

This little Drag and Drop demo shows how you modify the DataObject during a Drag and Drop operation. This is very useful if you want to provide user interaction before you do the final drop into the destination. A good use for this would be the creation of a toolbox to add items to a destination (form, listbox, grid, etc.) where you want to customize (give a name, additional configurations, etc.) before you drop them.

modify_drag_and_drop_data/DragDrop001.png

Background

This sample uses the standard Drag and Drop API from the .NET Framework, and basic functions and events like:

  • DoDragDrop (Method)
  • OnDragEnter (Event)
  • OnDragDrop

Concept / Description

The concept of the demo is to have a toolbox that can drop items to a standard ListBox. The toolbox consists of a Panel with three Labels on it (Item A, Item B, and Item C) that can be pushed with Drag and Drop into the ListBox.

The ListBox only accepts a DataObject of type String with content "Item A" or "Item C". Before the item is dropped into the ListBox and only if the option "Timestamp confirmation?" is activated, the user has to respond to say if he wants to append a timestamp to the string he is appending to the ListBox.

modify_drag_and_drop_data/DragDrop002.png

Code

So the first step is to create the DataObject from the Label when you start a Drag and Drop operation:

private void ItemALabel_MouseMove(object sender, MouseEventArgs e) 
{  
   if (e.Button == MouseButtons.Left)  
   {  
      ItemALabel.DoDragDrop(ItemALabel.Text, DragDropEffects.All);  
   }  
}  
  
private void ItemBLabel_MouseMove(object sender, MouseEventArgs e)  
{  
   if (e.Button == MouseButtons.Left)  
   {  
      ItemBLabel.DoDragDrop(ItemBLabel.Text, DragDropEffects.All);  
   }  
}  
  
private void ItemCLabel_MouseMove(object sender, MouseEventArgs e)  
{  
   if (e.Button == MouseButtons.Left)  
   {  
      ItemCLabel.DoDragDrop(ItemCLabel.Text, DragDropEffects.All);  
   }  
}

The ListBox has to decide if it will accept the drag and drop operation. Only DataObjects of type String with content "Item A" or "Item C" are accepted:

private void ListBoxControl_DragEnter(object sender, DragEventArgs e) 
{ 
   if (e.Data.GetFormats()[0] == "System.String") 
   { 
      String dropString = (String)e.Data.GetData("System.String"); 
 
      if ((dropString == "Item A") || (dropString == "Item C")) 
      { 
         e.Effect = DragDropEffects.Copy; 
      } 
      else 
      { 
         e.Effect = DragDropEffects.None; 
      } 
   } 
   else 
   { 
      e.Effect = DragDropEffects.None; 
   } 
}

Finally, on the Drop event, you can handle the DataObject modification:

private void MainListView_DragDrop(object sender, DragEventArgs e) 
{ 
   if (TimestampCheckBox.Checked) 
   { 
      if (MessageBox.Show("Do you want to add a timestamp?", 
      "Confirmation", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) 
      { 
         if (e.Data.GetFormats()[0] == "System.String") 
         { 
            String timestampedString = (String)e.Data.GetData("System.String"); 
            timestampedString = String.Format("{0} ({1})", 
            timestampedString, DateTime.Now); 
            e.Data.SetData(timestampedString); 
         } 
      } 
   } 
}

Conclusion

As you can see, with the Drag and Drop API from the .NET Framework, you have a lot of flexibility and you can handle a lot of different scenarios.

History

  • 15 December 2010: Initial revision
  • 22 December 2010: Updated source code to remove third party components

License

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

Share

About the Author

Klaus Stefan Gerber
Architect POHN IT-Consulting GmbH
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSouthmountain16-Sep-13 7:23 
GeneralCan't Compile PinmemberIzhar A.20-Dec-10 9:51 
GeneralRe: Can't Compile PinmemberStefan Gerber22-Dec-10 4:10 

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 | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 15 Oct 2012
Article Copyright 2010 by Klaus Stefan Gerber
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid