Providing Drag and Drop
In some ways, drag and drop performs the same task as the clipboard. Both allow one application
to give data to another. The difference is that the clipboard saves data for later delivery whereas
drag and drop delivers the data immediately and is done.
In this lesson, you learn how to add drag and drop to your program so it can interact with
other applications.
Note that drag and drop is only useful in some environments. This lesson explains
how to provide drag and drop in Windows Forms application (which is the focus
of this book). If you’re writing an application for some other platform, such as
an ASP.NET application that runs in a browser, the techniques described in this
chapter won’t work. In fact, for some applications drag and drop may not even
make sense.
UNDERSTANDING DRAG AND DROP EVENTS
There are two participants in a drag and drop operation: a drag source and a drop target.
The
drag source initiates a drag, for example when you right-click it. It determines what
data is in the drag and what kinds of operations are allowed on the drag such as a copy
or cut.
The
drop target is a potential recipient of a drag’s data. When the drag moves over
it, the target can decide whether it can accept the data in an offered operation such as
copy or cut.
To handle all of the potential interactions among the drag source, the drop target, and the
user, you can use several event handlers. Some of these events occur in the drag source and
others occur in the drop target.
33
596906c33.indd 377 4/7/10 12:34:38 PM
378
LESSON 33 Providing drag and droP
Table 33-1 summarizes the key drag source events.
TABLE 331
EVENT PURPOSE
GiveFeedback
The drag has entered a valid drop target. The source can indicate the
type of drop allowed. For example, it might allow Copy if the target is a
Label and allow Move or Copy if the target is a TextBox.
QueryContinueDrag
The keyboard or mouse button state has changed. The drag source can
decide whether to continue the drag, cancel the drag, or drop the data
immediately.
The drop target has more events than the drag source. The drag source merely provides data that
drop targets might want to accept. The drop target might need to provide a lot more interaction to
tell the user what’s happening.
Table 33-2 describes the events received by a drop target when data is dragged over it.
TABLE 332
EVENT PURPOSE
DragEnter
The drag is entering the target. The target can examine the type of data
available and set
e.Effect to indicate the types of drops it can handle.
It can also display some sort of highlight to tell the user that it can
accept the data and where it might land.
DragLeave
The drag has left the target. The target should remove any highlighting
or other hints that it displayed in
DragEnter.
DragOver
The drag is over the target. This event continues to fire a few times per
second until the drag leaves. For example, the target could change its
appearance to show exactly where the data would land if dropped. It
can also check things such as the keyboard state — it might allow a
Copy if the Ctrl key is pressed and a Move otherwise.
DragDrop
The user dropped the data on the target so the target should process
the data.
STARTING A DRAG
Starting a drag is fairly easy. First create an instance of the DataObject class to indicate the type of
data being dragged and to hold the data itself. Then simply call a control’s
DoDrag method, passing
it the
DataObject.
596906c33.indd 378 4/7/10 12:34:39 PM
Catching a Drop
379
The DragSource example program (available in this lesson’s code download) uses the following code
to start dragging text when you press the right mouse button down over its
dragLabel control:
// Start a drag.
private void dragLabel_MouseDown(object sender, MouseEventArgs e)
{
// If it’s not the right mouse button, do nothing.
if (e.Button != MouseButtons.Right) return;
// Make the data object.
DataObject data = new DataObject(DataFormats.Text, dragLabel.Text);
// Start the drag allowing only copy.
dragLabel.DoDragDrop(data, DragDropEffects.Copy);
}
The code first checks whether the right mouse button is pressed and exits if it is not. It then creates
a
DataObject. It passes the object’s constructor the value DataFormats.Text to indicate that it will
hold text data and the text that the object should hold.
Finally, the code calls the
Label control’s DoDrag method passing it the DataObject and the value
DragDropEffects.Copy to indicate that the drag allows only the copy operation.
This example doesn’t bother with the
GiveFeedback and QueryContinueDrag event handlers, so
that’s the extent of its participation in the drag.
The DoDrag function returns a DragDropEffects value that indicates what the
drop target decided to do with the data. For example, if the function returns
DragDropEffects.Move, then the user is performing a Move operation so the
drag source should remove the data from its application. For example, a file
explorer such as Windows Explorer would remove the file from the source
location and move it to the drop location.
Note that a drag is completely separate from the drop target. If a drop target can accept the data,
it is free to do so. That means you don’t need to wait until you read about the DropTarget example
program described in the next section to test the DragSource program. You can test the program
right now by using it to drag text data into Word, WordPad, or any other program that accepts
dropped text. (Notepad doesn’t know how to accept dropped text.)
CATCHING A DROP
Before a control can accept a drop, you must set the form’s AllowDrop property to true. If
AllowDrop is false, the form will not allow drops no matter what event handlers you create.
The only other thing a drop target must do is provide
DragEnter and DragDrop event handlers.
596906c33.indd 379 4/7/10 12:34:39 PM
380
LESSON 33 Providing drag and droP
The DragEnter event handler should examine the data available and set the event handler’s
e.Effect parameter to indicate whether it wants to allow the drop. The drag and drop system
automatically changes the mouse cursor to indicate the kind of drop that the target allows.
The DropTarget example program (available in this lesson’s code download) uses the following
DragEnter event handler:
// A drag entered. List available formats.
private void Form1_DragEnter(object sender, DragEventArgs e)
{
// Allow text data.
if (e.Data.GetDataPresent(DataFormats.Text))
{
// Only allow the Copy operation.
e.Effect = DragDropEffects.Copy;
}
}
This code checks whether the drag contains text data and, if it does, sets e.Effect to allow the
Copy operation.
The DragEnter event handler’s e.Effect parameter has the value
DragDropEffects.None by default, so you don’t need to set it if you want to
prevent a drop. In the DropTarget example program, if no text is available,
the program doesn’t allow any kind of drop operation.
The DragDrop event handler should use its e.Data parameter to see what data is available and to
get it. It should then set
e.Effect to indicate what operation it performed, such as Copy or Move so
the drag source knows what happened to the data.
The DropTarget example program uses the following
DragDrop event handler:
// Accept dropped data.
private void Form1_DragDrop(object sender, DragEventArgs e)
{
// Get the dropped data.
if (e.Data.GetDataPresent(DataFormats.Text))
{
dropLabel.Text = (string)e.Data.GetData(DataFormats.Text);
// Indicate that we copied.
e.Effect = DragDropEffects.Copy;
}
}
This code checks for text data. If text is available, the code gets it and displays it in a label. It
also sets
e.Effect = DragDropEffects.Copy to tell the drag source that the target performed a
Copy operation.
Note that accepting a drop is completely separate from the drag. If data is available in a format
that your program can use, you are free to use it. In this case that means you don’t need to use the
596906c33.indd 380 4/7/10 12:34:39 PM
Try It
381
DragSource example program described in the previous section to test the DropTarget example pro-
gram. You can also drag text from Word, WordPad, or any other program that knows how to start
dragging text. (Notepad doesnt know how to start a drag.)
TRY IT
In this Try It, you elaborate on the DragSource and DropTarget example programs. You modify
DragSource so it allows Copy or Move operations.
You make DropTarget perform Copy or Move operations depending on whether the Ctrl key is pressed
during the drop. You also provide feedback if the user changes the state of the Ctrl key during the drag.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson33 folder of the download.
Lesson Requirements
Copy the DragSource and DropTarget example programs into new directories.
In DragSource, allow Copy and Move operations. Check the result of
DoDragDrop and if the
operation was a Move, remove the text from the source
Label control.
In DropTarget, make the
DragEnter event handler allow Copy and Move operations.
In DropTarget, add a
DragOver event handler. The value e.KeyState & 8 is nonzero if the
Ctrl key is pressed. If Ctrl is pressed, allow Copy. If Ctrl is not pressed, allow Move.
In DropTarget, make the
DragDrop event handler check the state of the Ctrl key just as
DragOver does. If Ctrl is pressed, tell the drag source that the operation is a Copy. If Ctrl is
not pressed, tell the drag source that the operation is a Move.
Hints
To make DragSource allow either Move or Copy operations, use
DragDropEffects.Copy |
DragDropEffects.Move
.
To make DropTarget allow either Move or Copy operations, use
DragDropEffects.Copy |
DragDropEffects.Move
.
Step-by-Step
Copy the DragSource and DropTarget example programs into new directories.
1. This is straightforward.
596906c33.indd 381 4/7/10 12:34:40 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.254.192