Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms

Modifying DataObject During Drag and Drop Operation

Rate me:
Please Sign up or sign in to vote.
4.80/5 (3 votes)
14 Oct 2012CPOL2 min read 35.3K   701   28   3
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:

C#
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:

C#
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:

C#
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)


Written By
Architect POHN IT-Consulting GmbH
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Southmountain16-Sep-13 6:23
Southmountain16-Sep-13 6:23 
GeneralCan't Compile Pin
Izhar A.20-Dec-10 8:51
Izhar A.20-Dec-10 8:51 
GeneralRe: Can't Compile Pin
Klaus Stefan Gerber22-Dec-10 3:10
Klaus Stefan Gerber22-Dec-10 3:10 

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.