Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms
To perform drag-and-drop operations within Windows-based applications you must handle a series of events, most notably the DragEnter, DragLeave, and DragDrop events. By working with the information available in the event arguments of these events, you can easily facilitate drag-and-drop operations.
All drag-and-drop operations begin with dragging. The functionality to enable data to be collected when dragging begins is implemented in the DoDragDrop method.
In the following example, the MouseDown event is used to start the drag operation because it is the most intuitive (most drag-and-drop actions begin with the mouse button being depressed). However, remember that any event could be used to initiate a drag-and-drop procedure.
In the MouseDownevent for the control where the drag will begin, use the
DoDragDropmethod to set the data to be dragged and the allowed effect dragging will have. For more information, see Data and AllowedEffect.
The following example shows how to initiate a drag operation. The control where the drag begins is a Button control, the data being dragged is the string representing the Text property of the Button control, and the allowed effects are either copying or moving.
Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown Button1.DoDragDrop(Button1.Text, DragDropEffects.Copy Or DragDropEffects.Move) End Sub
Any data can be used as a parameter in the
DoDragDropmethod; in the example above, the Text property of the Button control was used (rather than hard-coding a value or retrieving data from a dataset) because the property was related to the location being dragged from (the Button control). Keep this in mind as you incorporate drag-and-drop operations into your Windows-based applications.
While a drag operation is in effect, you can handle the QueryContinueDrag event, which "asks permission" of the system to continue the drag operation. When handling this method, it is also the appropriate point for you to call methods that will have an effect on the drag operation, such as expanding a TreeNode in a TreeView control when the cursor hovers over it.
Once you have begun dragging data from a location on a Windows Form or control, you will naturally want to drop it somewhere. The cursor will change when it crosses an area of a form or control that is correctly configured for dropping data. Any area within a Windows Form or control can be made to accept dropped data by setting the AllowDrop property and handling the DragEnter and DragDrop events.
Set the AllowDrop property to true.
DragEnterevent for the control where the drop will occur, ensure that the data being dragged is of an acceptable type (in this case, Text). The code then sets the effect that will happen when the drop occurs to a value in the DragDropEffects enumeration. For more information, see Effect.
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.Text)) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub
You can define your own DataFormats by specifying your own object as the Object parameter of the SetData method. Be sure, when doing this, that the object specified is serializable. For more information, see ISerializable Interface.
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString End Sub
Additionally, you can work with the KeyState property, so that, depending on keys depressed during the drag-and-drop operation, certain effects occur (for example, it is standard to copy the dragged data when the CTRL key is pressed).