Drag and drop to move or copy text while providing feedback in C#

These examples explain the basics of drag and drop:

Those examples use:

  • The DoDragDrop method to start a drag
  • The DragEnter event to tell the drag and drop system what kinds of drop a control can receive
  • The DragDrop event to accept the drop

This example demonstrates two new events: DragOver and DragLeave.

The DragOver event fires repeatedly as long as the drag remains over a control. This lets the program check the state of the system to see if it should change how it might accept a drag. In particular, many applications check the state of the Shift key and allow a Copy of it is pressed and a Move otherwise. That's what the following code does.

// Check the key state. Allow copy if Shift is pressed.
private void lblDropTarget_DragOver(object sender, DragEventArgs e)
{
const int KEY_SHIFT = 4;

if ((e.KeyState & KEY_SHIFT) != 0)
{
// Shift is pressed. Allow move.
e.Effect = DragDropEffects.Move;
}
else
{
// Shift is not pressed. Allow copy.
e.Effect = DragDropEffects.Copy;
}
}

This code checks the e.KeyState parameter to see if Shift is pressed. It then sets e.Effect to Move or Copy as appropriate.

The DragLeave event fires when a drag leaves a control. The idea here is that you might want to do something to the control to highlight it when the drag enters the control in the DragEnter event. This example changes the drop target's background color to light yellow. (If you look at the picture above you'll see that the drop target has a light yellow background.) When the drag moves off of the control, the following code resets the control's color so it is no longer highlighted.

// Remove the background color highlight.
private void lblDropTarget_DragLeave(object sender, EventArgs e)
{
lblDropTarget.BackColor = SystemColors.Control;
}

The last two new ideas in this example are in how the drag source performs the drag in the following code.

// Start a copy or move drag of text.
private void lblDragSource_MouseDown(object sender, MouseEventArgs e)
{
// Start the drag if it's the right mouse button.
if (e.Button == MouseButtons.Right)
{
if (lblDragSource.DoDragDrop("Here's some text!",
DragDropEffects.Copy | DragDropEffects.Move)
== DragDropEffects.Move)
{
// The recipient moved the text.
// Remove it from here.
lblDragSource.Text = "";
}
}
}

In this example, the drag source allows a drop target to accept either a Copy or Move. To do that, the code passes the bitwise Or of the two operations DragDropEffects.Copy | DragDropEffects.Move into the call to DoDragDrop.

Second, when the drop target accepts the drop, the drag source checks the result returned by DoDragDrop to see which operation was accepted. In this example, if the operation was a Move, then the drag source removes the data from itself.

Drag and drop is covered in my book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.