Control.DragDrop 事件

定义

拖放操作完成时发生。

C#
public event System.Windows.Forms.DragEventHandler DragDrop;
C#
public event System.Windows.Forms.DragEventHandler? DragDrop;

事件类型

示例

此代码摘录演示如何使用 DragDrop 事件。 有关完整代码示例, DoDragDrop 请参阅 方法。

C#
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";
}

注解

XDragEventArgsY 属性位于屏幕坐标中,而不是客户端坐标。 以下 Visual C# 代码行将属性转换为客户端 Point

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

备注

在低于 .NET Framework 2.0 的版本中,如果在 Windows 窗体上放置 UserControl 了 和 DragEnterDragDrop 事件,并在设计时将和 事件拖放到 UserControl 中,DropDrop则会引发 和 DropEnter 事件。 但是,关闭并重新打开解决方案时, DragEnter 不会再次引发 和 DragDrop 事件。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅