Control.DragDrop Evento

Definizione

Si verifica quando viene completata un'operazione di trascinamento e rilascio.Occurs when a drag-and-drop operation is completed.

public:
 event System::Windows::Forms::DragEventHandler ^ DragDrop;
public event System.Windows.Forms.DragEventHandler DragDrop;
member this.DragDrop : System.Windows.Forms.DragEventHandler 
Public Custom Event DragDrop As DragEventHandler 

Tipo evento

DragEventHandler

Esempio

Questo estratto di codice illustra l'uso dell' DragDrop evento.This code excerpt demonstrates using the DragDrop event. Vedere il DoDragDrop metodo per l'esempio di codice completo.See the DoDragDrop method for the complete code example.

void ListDragTarget_DragDrop( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ e )
{
   // Ensure that the list item index is contained in the data.
   if ( e->Data->GetDataPresent( System::String::typeid ) )
   {
      Object^ item = dynamic_cast<Object^>(e->Data->GetData( System::String::typeid ));
      
      // Perform drag-and-drop, depending upon the effect.
      if ( e->Effect == DragDropEffects::Copy || e->Effect == DragDropEffects::Move )
      {
         // Insert the item.
         if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches )
                        ListDragTarget->Items->Insert( indexOfItemUnderMouseToDrop, item );
         else
                        ListDragTarget->Items->Add( item );
      }
   }

   // Reset the label text.
   DropLocationLabel->Text = "None";
}
private void ListDragTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
{
    // Ensure that the list item index is contained in the data.
    if (e.Data.GetDataPresent(typeof(System.String))) {

        Object item = (object)e.Data.GetData(typeof(System.String));

        // Perform drag-and-drop, depending upon the effect.
        if (e.Effect == DragDropEffects.Copy ||
            e.Effect == DragDropEffects.Move) {
        
            // Insert the item.
            if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
                ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item);
            else
                ListDragTarget.Items.Add(item);
        }
    }
    // Reset the label text.
    DropLocationLabel.Text = "None";
}
Private Sub ListDragTarget_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragDrop
    ' Ensures that the list item index is contained in the data.

    If (e.Data.GetDataPresent(GetType(System.String))) Then

        Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)

        ' Perform drag-and-drop, depending upon the effect.
        If (e.Effect = DragDropEffects.Copy Or _
            e.Effect = DragDropEffects.Move) Then

            ' Insert the item.
            If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then
                ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item)
            Else
                ListDragTarget.Items.Add(item)

            End If
        End If
        ' Reset the label text.
        DropLocationLabel.Text = "None"
    End If
End Sub

Commenti

Le X Y proprietà e di DragEventArgs sono nelle coordinate dello schermo, non nelle coordinate del client.The X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates. La riga seguente di codice Visual C# converte le proprietà in un client Point .The following line of Visual C# code converts the properties to a client Point.

Point clientPoint = targetControl.PointToClient(new Point(de.X, de.Y));  

Nota

Nelle versioni precedenti a .NET Framework 2.0.NET Framework 2.0 , se si inserisce un UserControl oggetto DragEnter con DragDrop gli eventi e in un Windows Form e si trascina un elemento in UserControl in fase di progettazione DropDrop , DropEnter vengono generati gli eventi e.In versions earlier than .NET Framework 2.0.NET Framework 2.0, if you put a UserControl with DragEnter and DragDrop events on a Windows Form and drag and drop something onto the UserControl at design time, the DropDrop and DropEnter events are raised. Tuttavia, quando si chiude e si riapre la soluzione, DragEnter gli DragDrop eventi e non vengono generati nuovamente.However, when you close and reopen the solution, the DragEnter and DragDrop events are not raised again.

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.For more information about handling events, see Handling and Raising Events.

Si applica a

Vedi anche