ToolStripItem.DragLeave ToolStripItem.DragLeave ToolStripItem.DragLeave ToolStripItem.DragLeave Event


当用户拖动某项并且鼠标指针不再悬停在此项的工作区上方时发生。Occurs when the user drags an item and the mouse pointer is no longer over the client area of this item.

 event EventHandler ^ DragLeave;
public event EventHandler DragLeave;
member this.DragLeave : EventHandler 
Public Custom Event DragLeave As EventHandler 


下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中, 事件处理程序报告DragLeave事件的发生。In the example, an event handler reports on the occurrence of the DragLeave event. 此报表可帮助你了解何时发生事件, 并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告多个事件或频繁发生的事件, 请考虑MessageBox.ShowConsole.WriteLine消息替换为多行或将TextBox其追加到多行。To report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行示例代码, 请将其粘贴到一个项目中, 该项目包含从ToolStripItem继承的类型的实例, 如ToolStripMenuItem ToolStripButton或。To run the example code, paste it into a project that contains an instance of a type that inherits from ToolStripItem, such as a ToolStripButton or ToolStripMenuItem. 然后命名该实例ToolStripItem1 , 并确保事件处理程序DragLeave与事件相关联。Then name the instance ToolStripItem1 and ensure that the event handler is associated with the DragLeave event.

private void ToolStripItem1_DragLeave(Object sender, EventArgs e) {

   MessageBox.Show("You are in the ToolStripItem.DragLeave event.");

Private Sub ToolStripItem1_DragLeave(sender as Object, e as EventArgs) _ 
     Handles ToolStripItem1.DragLeave

   MessageBox.Show("You are in the ToolStripItem.DragLeave event.")

End Sub


DragLeave用户将光标拖出控件或用户取消当前拖放操作时, 将引发事件。The DragLeave event is raised when the user drags the cursor out of the control or the user cancels the current drag-and-drop operation.

以下备注说明了如何和何时引发与拖放操作相关的事件。The following remarks describe how and when events related to drag-and-drop operations are raised.

DoDragDrop方法确定当前游标位置下的项。The DoDragDrop method determines the item under the current cursor location. 然后, 它会检查该项是否为有效的拖放目标。It then checks to see if the item is a valid drop target.

如果该项是有效的拖放目标, 则GiveFeedback会在指定了拖放效果的情况下引发事件。If the item is a valid drop target, the GiveFeedback event is raised with the drag-and-drop effect specified. 有关拖放效果的列表,请参见 DragDropEffects 枚举。For a list of drag-and-drop effects, see the DragDropEffects enumeration.

按以下方式跟踪鼠标光标位置、键盘状态和鼠标按钮状态的更改:Changes in the mouse cursor position, keyboard state, and mouse button state are tracked in the following manner:

  • 如果用户移出一个窗口,则引发 DragLeave 事件。If the user moves out of a window, the DragLeave event is raised.

  • 如果鼠标进入另一个项, 则DragEnter引发该控件的。If the mouse enters another item, the DragEnter for that control is raised.

  • 如果鼠标移动但停留在同一项中, DragOver则会引发事件。If the mouse moves but stays within the same item, the DragOver event is raised.

如果键盘或鼠标按钮状态发生了更改, QueryContinueDrag则会引发事件, 并确定是继续拖动、删除数据还是根据事件的Action QueryContinueDragEventArgs属性的值来取消操作。If there is a change in the keyboard or mouse button state, the QueryContinueDrag event is raised and determines whether to continue the drag, to drop the data, or to cancel the operation based on the value of the Action property of the event's QueryContinueDragEventArgs.

如果的DragAction值为Continue, DragOver则会引发事件以继续操作, 并且GiveFeedback事件将以新效果引发, 以便可以设置适当的视觉反馈。If the value of DragAction is Continue, the DragOver event is raised to continue the operation and the GiveFeedback event is raised with the new effect so appropriate visual feedback can be set. 有关有效放置效果的列表,请参见 DragDropEffects 枚举。For a list of valid drop effects, see the DragDropEffects enumeration.

DragOverGiveFeedback事件成对出现, 因此, 当鼠标移动到拖放目标上时, 将为用户提供有关鼠标位置的最新反馈, 如下所示:The DragOver and GiveFeedback events are paired so that as the mouse moves across the drop target, the user is given the most up-to-date feedback on the mouse's position, as follows:

  • 如果的DragAction值为Drop, 则放置效果值将返回到源, 以便源应用程序可以对源数据执行适当的操作; 例如, 如果是移动操作, 则剪切数据。If the value of DragAction is Drop, the drop effect value is returned to the source, so the source application can perform the appropriate operation on the source data; for example, cut the data if the operation was a move.

  • 如果的DragAction值为Cancel, DragLeave则引发事件。If the value of DragAction is Cancel, the DragLeave event is raised.