ToolStripItem.DragOver ToolStripItem.DragOver ToolStripItem.DragOver ToolStripItem.DragOver Event

定义

当用户将某项拖动到此项的工作区上方时发生。Occurs when the user drags an item over the client area of this item.

public:
 event System::Windows::Forms::DragEventHandler ^ DragOver;
[System.ComponentModel.Browsable(false)]
public event System.Windows.Forms.DragEventHandler DragOver;
member this.DragOver : System.Windows.Forms.DragEventHandler 
Public Custom Event DragOver As DragEventHandler 
属性

示例

下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中, 事件处理程序报告DragOver事件的发生。In the example, an event handler reports on the occurrence of the DragOver 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 , 并确保事件处理程序DragOver与事件相关联。Then name the instance ToolStripItem1 and ensure that the event handler is associated with the DragOver event.

private void ToolStripItem1_DragOver(Object sender, DragEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "Data", e.Data );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "KeyState", e.KeyState );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "X", e.X );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Y", e.Y );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "AllowedEffect", e.AllowedEffect );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Effect", e.Effect );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "DragOver Event" );
}
Private Sub ToolStripItem1_DragOver(sender as Object, e as DragEventArgs) _ 
     Handles ToolStripItem1.DragOver

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "Data", e.Data)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "KeyState", e.KeyState)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "X", e.X)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Y", e.Y)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "AllowedEffect", e.AllowedEffect)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Effect", e.Effect)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"DragOver Event")

End Sub

注解

在拖放操作过程中, 当鼠标光标移到控件的边界内时, 会引发事件。DragOverThe DragOver event is raised when the mouse cursor moves within the bounds of the control during a 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.

适用于