Adding Support for Multiple Drag-and-Drop Effects


Adding support for multiple drag-and-drop effects requires additional updates to both the drag source and the drop target controls.

Adding Multiple Drag-and-Drop Effects to the Drag Source

On the drag source, you must specify which effects are allowed and check the resulting effect after the DoDragDrop method returns:

// MainForm.cs partial class MainForm : Form {   ...   void dragSourceLabel_MouseDown(object sender, MouseEventArgs e) {     // Start a drag and drop operation     DragDropEffects supportedEffects = DragDropEffects.Copy |                                           DragDropEffects.Move;     string dragData = this.dragSourceLabel.Text;     DragDropEffects dragEffect =       DoDragDrop(dragData, supportedEffects);     // If the effect was move, remove the text of the button     // If the effect was a copy, we don't have anything to do     if (dragEffect == DragDropEffects.Move) {       this.dragSourceLabel.Text = "";     }   } }


This code examines the drag-and-drop effect returned by DoDragDrop and responds appropriately. In this case, that simply requires checking whether a Move operation occurred and, if so, enacting it.

Adding Multiple Drag-and-Drop Effects to the Drop Target

The DragDropEffects value returned by DoDragDrop is actually provided by the drop target. You need to establish which keys were pressed while the drag-and-drop operation took place, and to do that you examine the KeyState property of the DragEventArgs class. KeyState is a set of flags that determines which keys are being pressed. By Windows convention, the lack of modifier keys indicates a Move, the Ctrl modifier indicates a Copy, and the Ctrl+Shift modifier indicates a Link (which your application may or may not support).

Unfortunately, the KeyState property is an integer, and Windows Forms provides no data type for checking the flags. So, you need to write your own, such as this KeyState enumeration:[2]

[2] The Flags attribute makes instances of the KeyState enumeration show up in a friendlier manner, such as "LeftMouse, CtrlKey" instead of "9," and supports bitwise operators like "|" and "&."

// KeyState Values (not available in Windows Forms) [Flags] enum KeyState {   LeftMouse = 1,   RightMouse = 2,   ShiftKey = 4,   CtrlKey = 8,   MiddleMouse = 16,   AltKey = 32, }


Because users may change the keys they're pressing at any time to get the effect they're looking for, you should specify the drop effect for the drag operation they are trying to do. To do this, you check the DragEnter and DragOver events:

// MainForm.cs partial class MainForm : Form {   ...   void dropTargetTextBox_DragEnter(object sender, DragEventArgs e) {     SetDropEffect(e);   }   void dropTargetTextBox_DragOver(object sender, DragEventArgs e) {     SetDropEffect(e);   }   void SetDropEffect(DragEventArgs e) {     KeyState keyState = (KeyState)e.KeyState;     // If the data is a string, we can handle it     if (e.Data.GetDataPresent(typeof(string))) {       // If only Ctrl is pressed, copy it       if ((keyState & KeyState.CtrlKey) == KeyState.CtrlKey) {         e.Effect = DragDropEffects.Copy;       }       else { // Else, move it         e.Effect = DragDropEffects.Move;       }   }     // We don't like the data, so do not allow anything     // e.Effect = DragDropEffects.None by default   } }


The SetDropEffect method makes sure that the data is a string because that is all we are expecting. If it finds a string, it tests to see whether the Ctrl key is pressed. If it is, it specifies that the operation is a copy; otherwise, it specifies that it will do a move.

Figure E.5 shows what the drag operation now looks like over the text box without the Ctrl key pressed, indicating a move effect.

Figure E.5. Dragging Without Ctrl, Causing a Move


Figure E.6 shows the same operation with the Ctrl key pressed, indicating a copy effect.

Figure E.6. Dragging with Ctrl, Causing a Copy


In our sample, a move is indicated when the user drops the data with no modifiers, and the text is removed from the drag source label when it drops the text to the text box, as shown in Figure E.7.

Figure E.7. After a Drag-and-Drop Move Operation


Drag and drop is a great way to allow your mouse-oriented users to directly manipulate the data that your application presents without an undue development burden on you.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net