共用方式為


Control.DragDrop 事件

定義

發生於拖放作業完成時。

public:
 event System::Windows::Forms::DragEventHandler ^ DragDrop;
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 

事件類型

範例

此程式碼摘錄示範如何使用 DragDrop 事件。 如需完整的程式碼範例, DoDragDrop 請參閱 方法。

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, DragEventArgs e)
{
    // Ensure that the list item index is contained in the data.
    if (e.Data.GetDataPresent(typeof(System.String)))
    {
        Object item = 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

備註

XDragEventArgsY 屬性位於螢幕座標中,而不是用戶端座標。 下列一行 Visual C# 程式碼會將屬性轉換成用戶端 Point

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

注意

在 .NET Framework 2.0 之前的版本中,如果您在 Windows Form 上放置 UserControl with DragEnterDragDrop 事件,並在設計階段 DropDrop 將專案拖放到 UserControl ,則會引發 和 DropEnter 事件。 不過,當您關閉並重新開啟解決方案時, DragEnter 不會再次引發 和 DragDrop 事件。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱