DragEventArgs Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
The DragDrop event occurs when the user completes a drag-and-drop operation by dragging an object over the control and then dropping it onto the control by releasing the mouse button. The DragEnter event occurs when the user moves the mouse pointer onto the control while dragging an object with the mouse. The DragOver event occurs when the user moves the mouse pointer over the control while dragging an object with the mouse.
A DragEventArgs object specifies any data associated with this event; the current state of the SHIFT, CTRL, and ALT keys; the location of the mouse pointer; and the drag-and-drop effects allowed by the source and target of the drag event.
For information about the event model, see Events and Delegates.
The following example demonstrates a drag-and-drop operation between two ListBox controls. The example calls the DoDragDrop method when the drag action starts. The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. The IndexFromPoint method is used to determine the index of the item to drag during the MouseDown event.
The example also demonstrates using custom cursors for the drag-and-drop operation. The example assumes that two cursor files, 3dwarro.cur and 3dwno.cur, exist in the application directory, for the custom drag and no-drop cursors, respectively. The custom cursors will be used if the UseCustomCursorsCheckCheckBox is checked. The custom cursors are set in the GiveFeedback event handler.
The keyboard state is evaluated in the DragOver event handler for the right ListBox, to determine what the drag operation will be based upon state of the SHIFT, CTRL, ALT, or CTRL+ALT keys. The location in the ListBox where the drop would occur is also determined during the DragOver event. If the data to drop is not a String, then the DragEventArgs.Effect is set to DragDropEffects.None. Finally, the status of the drop is displayed in the DropLocationLabelLabel.
The data to drop for the right ListBox is determined in the DragDrop event handler and the String value is added at the appropriate place in the ListBox. If the drag operation moves outside the bounds of the form, then the drag-and-drop operation is canceled in the QueryContinueDrag event handler.
This code excerpt demonstrates using the DragEventArgs class. See the DoDragDrop method for the complete code example.
Private Sub ListDragTarget_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragOver ' Determine whether string data exists in the drop data. If not, then ' the drop effect reflects that the drop cannot occur. If Not (e.Data.GetDataPresent(GetType(System.String))) Then e.Effect = DragDropEffects.None DropLocationLabel.Text = "None - no string data." Return End If ' Set the effect based upon the KeyState. If ((e.KeyState And (8 + 32)) = (8 + 32) And _ (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then ' KeyState 8 + 32 = CTL + ALT ' Link drag-and-drop effect. e.Effect = DragDropEffects.Link ElseIf ((e.KeyState And 32) = 32 And _ (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then ' ALT KeyState for link. e.Effect = DragDropEffects.Link ElseIf ((e.KeyState And 4) = 4 And _ (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then ' SHIFT KeyState for move. e.Effect = DragDropEffects.Move ElseIf ((e.KeyState And 8) = 8 And _ (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then ' CTL KeyState for copy. e.Effect = DragDropEffects.Copy ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then ' By default, the drop action should be move, if allowed. e.Effect = DragDropEffects.Move Else e.Effect = DragDropEffects.None End If ' Gets the index of the item the mouse is below. ' The mouse locations are relative to the screen, so they must be ' converted to client coordinates. indexOfItemUnderMouseToDrop = _ ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(New Point(e.X, e.Y))) ' Updates the label text. If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then DropLocationLabel.Text = "Drops before item #" & (indexOfItemUnderMouseToDrop + 1) Else DropLocationLabel.Text = "Drops at the end." End If End Sub
private void listDragTarget_DragOver(Object sender,
System.Windows.Forms.DragEventArgs e)
{
// Determine whether string data exists in the drop data. If not, then
// the drop effect reflects that the drop cannot occur.
if (!(e.get_Data().GetDataPresent(String.class.ToType()))) {
e.set_Effect(DragDropEffects.None);
dropLocationLabel.set_Text("None - no string data.");
return;
}
// Set the effect based upon the KeyState.
if ((e.get_KeyState() & 8 + 32) == 8 + 32 && (e.get_AllowedEffect()
& DragDropEffects.Link) == DragDropEffects.Link) {
// KeyState 8 + 32 = CTL + ALT
// Link drag-and-drop effect.
e.set_Effect(DragDropEffects.Link);
}
else {
if ((e.get_KeyState() & 32) == 32 && (e.get_AllowedEffect()
& DragDropEffects.Link) == DragDropEffects.Link) {
// ALT KeyState for link.
e.set_Effect(DragDropEffects.Link);
}
else {
if ((e.get_KeyState() & 4) == 4 && (e.get_AllowedEffect()
& DragDropEffects.Move) == DragDropEffects.Move) {
// SHIFT KeyState for move.
e.set_Effect(DragDropEffects.Move);
}
else {
if ((e.get_KeyState() & 8) == 8 && (e.get_AllowedEffect()
& DragDropEffects.Copy) == DragDropEffects.Copy) {
// CTL KeyState for copy.
e.set_Effect(DragDropEffects.Copy);
}
else {
if ((e.get_AllowedEffect() & DragDropEffects.Move)
== DragDropEffects.Move) {
// By default, the drop action should be move,
//if allowed.
e.set_Effect(DragDropEffects.Move);
}
else {
e.set_Effect(DragDropEffects.None);
}
} // Get the index of the item the mouse is below.
}
} // The mouse locations are relative to the screen, so they
// must be converted to client coordinates.
}
indexOfItemUnderMouseToDrop = listDragTarget.IndexFromPoint(
listDragTarget.PointToClient(new Point(e.get_X(), e.get_Y())));
// Updates the label text.
if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) {
dropLocationLabel.set_Text("Drops before item #"
+ (indexOfItemUnderMouseToDrop + 1));
}
else {
dropLocationLabel.set_Text("Drops at the end.");
}
} //listDragTarget_DragOver
The following example illustrates how DragEventArgs are passed between the source and target of a drag-and-drop operation. In this example, a ListBox control is the source of the data, and the RichTextBox control is the target. The example assumes that the ListBox control has been populated with a list of valid filenames. When the user drags one of the displayed filenames from the ListBox control onto the RichTextBox control, the file referenced in the filename is opened.
The operation is initiated in the ListBox control's MouseDown event. In the DragEnter event handler, the example uses the GetDataPresent method to verify that the data is in a format that the RichTextBox control can display and then sets the DragDropEffects property to specify that data should be copied from the source control to the target control. Finally, the RichTextBox control's DragDrop event handler uses the GetData method to retrieve the filename to open.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Sets the AllowDrop property so that data can be dragged onto the control. RichTextBox1.AllowDrop = True ' Add code here to populate the ListBox1 with paths to text files. End Sub Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter ' If the data is text, copy the data to the RichTextBox control. If (e.Data.GetDataPresent("Text")) Then e.Effect = DragDropEffects.Copy End If End Sub Private Overloads Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop ' Loads the file into the control. RichTextBox1.LoadFile(e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText) End Sub Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown Dim Lb As ListBox Dim Pt As New Point(e.X, e.Y) Dim Index As Integer ' Determines which item was selected. Lb = sender Index = Lb.IndexFromPoint(Pt) ' Starts a drag-and-drop operation with that item. If Index >= 0 Then Lb.DoDragDrop(Lb.Items(Index), DragDropEffects.Link) End If End Sub
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.