ToolStripItem.DragOver 事件

定義

發生於使用者將項目拖過這個項目的工作區時。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. 若要報告多個事件或經常發生的事件,請考慮使用 Console.WriteLine 取代 MessageBox.Show,或將訊息附加到多行 TextBoxTo 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之類型實例的專案,例如 ToolStripButtonToolStripMenuItemTo 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

備註

在拖放作業期間,當滑鼠游標在控制項的範圍內移動時,就會引發 DragOver 事件。The 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.

  • 如果滑鼠進入另一個專案,則會引發該控制項的 DragEnterIf 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 事件,並決定是否要繼續拖曳、卸載資料,或根據事件 QueryContinueDragEventArgsAction 屬性值來取消作業。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.

適用於