Title: Drag and drop text with feedback in C#
These examples explain the basics of drag and drop:
Those examples use these methods and events:
- 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. For example, 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 and then undo the highlight when the drag leaves the control. 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;
}
Note that you probably also need to undo the highlight if the user drops the text on the control. This example uses the following code to accept a drop.
// Accept the drop.
private void lblDropTarget_DragDrop(object sender,
DragEventArgs e)
{
// Accept the data.
lblDropTarget.Text =
(string)e.Data.GetData(DataFormats.Text);
lblDropTarget.BackColor = SystemColors.Control;
}
The last two new ideas in this example are shown in the following code, which the drag source uses to perform the drag.
// 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, if 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 so the text moves from the source to the target.
Download the example to experiment with it and to see additional details.
|