共用方式為


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.

Dragging Data

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.

NoteNote

Certain controls have custom drag-specific events. The ListView and TreeView controls, for example, have an ItemDrag event.

To start a drag operation

  • In the MouseDown event for the control where the drag will begin, use the DoDragDrop method 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
    
    private void button1_MouseDown(object sender, 
    System.Windows.Forms.MouseEventArgs e)
    {
       button1.DoDragDrop(button1.Text, DragDropEffects.Copy | 
          DragDropEffects.Move);
    }
    
    private void button1_MouseDown(Object sender,
    System.Windows.Forms.MouseEventArgs e)
    {
       button1.DoDragDrop(button1.get_Text(), DragDropEffects.Copy |
          DragDropEffects.Move);
    }
    
    NoteNote

    Any data can be used as a parameter in the DoDragDrop method; 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.

Dropping Data

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.

To perform a drop

  1. Set the AllowDrop property to true.

  2. In the DragEnter event 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
    
    private void textBox1_DragEnter(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       if (e.Data.GetDataPresent(DataFormats.Text)) 
          e.Effect = DragDropEffects.Copy;
       else
          e.Effect = DragDropEffects.None;
    }
    
    private void textBox1_DragEnter(Object sender,
    System.Windows.Forms.DragEventArgs e)
    {
       if (e.get_Data().GetDataPresent(DataFormats.Text))
          e.set_Effect(DragDropEffects.Copy);
       else
          e.set_Effect(DragDropEffects.None);
    }
    
    NoteNote

    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.

  3. In the DragDrop event for the control where the drop will occur, use the GetData method to retrieve the data being dragged. For more information, see DtaObject.Data Property.

    In the example below, a TextBox control is the control being dragged to (where the drop will occur). The code sets the Text property of the TextBox control equal to the data being dragged.

    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
    
    private void textBox1_DragDrop(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
    }
    
    private void textBox1_DragDrop(Object sender,
    System.Windows.Forms.DragEventArgs e)
    {
       textBox1.set_Text
    (e.get_Data().GetData(DataFormats.Text).ToString());
    }
    
    NoteNote

    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).

See Also

Tasks

How to: Add Data to the Clipboard
How to: Retrieve Data from the Clipboard

Other Resources

Drag-and-Drop Operations and Clipboard Support