382
LESSON 33 Providing drag and droP
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.
1. Pass the DoDragDrop function the parameter DragDropEffects.Copy |
DragDropEffects.Move
for the allowed operations.
2. Check the result returned by DoDragDrop. If the result is DragDropEffects.Move,
clear the
Label control.
3. The code should look something like this:
// 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 copy and move.
if (dragLabel.DoDragDrop(data,
DragDropEffects.Copy | DragDropEffects.Move)
== DragDropEffects.Move)
{
// This is a Move. Remove the data from this application.
dragLabel.Text = “”;
}
}
In DropTarget, make the
DragEnter event handler allow Copy and Move operations.
1. In the DragEnter event handler, if text data is available set e.Effect =
DragDropEffects.Copy | DragDropEffects.Move
to allow both Copy and Move
operations. The code should look something like this:
// 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 and Move operations.
e.Effect = DragDropEffects.Copy | DragDropEffects.Move;
}
}
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.
1. Add the DragOver event handler. If Ctrl is pressed, set e.Effect =
DragDropEffects.Copy
. If Ctrl is not pressed, set e.Effect = DragDropEffects.Move.
// Allow copy or move depending on the Ctrl key state.
private void Form1_DragOver(object sender, DragEventArgs e)
596906c33.indd 382 4/7/10 12:34:40 PM
Try It
383
{
const int keyCtrl = 8;
if ((e.KeyState & keyCtrl) != 0)
{
// Ctrl is pressed. Allow Copy.
e.Effect = DragDropEffects.Copy;
}
else
{
// Ctrl is not pressed. Allow Move.
e.Effect = DragDropEffects.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.
1. Display the data as before.
2. Determine whether Ctrl is pressed. If Ctrl is pressed, set e.Effect = DragDropEffects
.Copy
. If Ctrl is not pressed, set e.Effect = DragDropEffects.Move.
// Accept dropped data.
private void Form1_DragDrop(object sender, DragEventArgs e)
{
const int keyCtrl = 8;
// Get the dropped data.
if (e.Data.GetDataPresent(DataFormats.Text))
{
dropLabel.Text = (string)e.Data.GetData(DataFormats.Text);
// Tell the drag source what operation we performed.
if ((e.KeyState & keyCtrl) != 0)
{
// Ctrl is pressed. Allow Copy.
e.Effect = DragDropEffects.Copy;
}
else
{
// Ctrl is not pressed. Allow Move.
e.Effect = DragDropEffects.Move;
}
}
}
Please select Lesson 33 on the DVD to view the video that accompanies this lesson.
596906c33.indd 383 4/7/10 12:34:40 PM
384
LESSON 33 Providing drag and droP
EXERCISES
1. Copy this lesson’s Try It (both the DragSource and DropTarget programs). Modify the
DropTarget program in these ways:
Add a
ListBox named formatsListBox. In the program’s DragEnter event handler,
loop through the string array returned by
e.Data.GetFormats and list the available
formats in the
ListBox.
In the
DragEnter event handler, allow the Copy operation for the Text and
FileDrop types of data.
Add a
DragLeave event handler that clears formatsListBox.
In the
DragDrop event handler, display text in a TextBox as before. If the dropped
data is a
FileDrop, it is an array of strings. Loop through the array and display the
dropped filenames in a
ListBox named dropListBox. Test this feature by dragging
files from Windows Explorer onto the program.
2. Copy the program you built for the Try It in Lesson 32 (or download Lesson 32’s version
from the book’s web site). Add the ability for the program to accept a dropped file list instead
of only getting a file list from the clipboard.
Hint: Don’t forget to set the forms
AllowDrop property to true.
Hint: It would be nice to move the code that backs up the files into a new
BackupFiles
function so you could call it for a file list taken from the clipboard or from drag and drop.
Unfortunately the clipboard’s file list is a
StringCollection but drag and drop’s file list is
an array of strings. Because they have different data types, you cannot make
BackupFiles
handle them both easily.
To solve this problem, make
BackupFiles take a filenames parameter of the non-generic
type
System.Collections.IList. The clipboard data implements this interface, so you can
simply pass it to
BackupFiles. To pass the drag and drop data to BackupFiles, simply cast
it into the
System.Collections.IList type.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson33 folder.
596906c33.indd 384 4/7/10 12:34:41 PM
Click here to Play
..................Content has been hidden....................

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