Control.DragDrop 事件
定义
拖放操作完成时发生。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
事件类型
示例
此代码摘录演示了如何使用 DragDrop 事件。This code excerpt demonstrates using the DragDrop event. DoDragDrop有关完整的代码示例,请参阅方法。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
注解
X的和 Y 属性 DragEventArgs 处于屏幕坐标中,而不是客户端坐标。The X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates. 以下 Visual c # 代码行将属性转换为客户端 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));
备注
在早于的版本中 .NET Framework 2.0.NET Framework 2.0 ,如果在 UserControl DragEnter DragDrop Windows 窗体上放置了 with 和事件,并在设计时将某些内容拖放到,则 UserControl DropDrop
DropEnter
会引发和事件。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. 但是,在关闭并重新打开解决方案时, DragEnter DragDrop 不会再次引发和事件。However, when you close and reopen the solution, the DragEnter and DragDrop events are not raised again.
有关处理事件的详细信息,请参阅 处理和引发事件。For more information about handling events, see Handling and Raising Events.