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

注釈

Xの プロパティと Y プロパティDragEventArgsは、クライアント座標ではなく画面座標内にあります。 次の Visual C# コード行は、プロパティをクライアント Pointに変換します。

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

Note

.NET Framework 2.0 より前のバージョンでは、Windows フォームに with イベントと DragDrop イベントを配置UserControlし、デザイン時に 何かを にUserControlドラッグ アンド ドロップすると、 DropDrop イベントと DropEnter イベントが発生します。DragEnter ただし、ソリューションを閉じて再度開くと、 DragEnter イベントと DragDrop イベントは再び発生しません。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください