ToolStripItem.DragEnter Event

Definition

Occurs when the user drags an item into the client area of this item.

public:
 event System::Windows::Forms::DragEventHandler ^ DragEnter;
[System.ComponentModel.Browsable(false)]
public event System.Windows.Forms.DragEventHandler DragEnter;
[System.ComponentModel.Browsable(false)]
public event System.Windows.Forms.DragEventHandler? DragEnter;
[<System.ComponentModel.Browsable(false)>]
member this.DragEnter : System.Windows.Forms.DragEventHandler 
Public Custom Event DragEnter As DragEventHandler 

Event Type

Attributes

Examples

The following code example shows how to convert the X and Y properties to a client Point. This code example is part of a larger example provided for the ToolStripRenderer class.

// This method defines the DragOver event behavior. 
protected override void OnDragOver(DragEventArgs dea)
{
    base.OnDragOver(dea);

    // Get the ToolStripButton control 
    // at the given mouse position.
    Point p = new Point(dea.X, dea.Y);
    ToolStripButton item = this.GetItemAt(
        this.PointToClient(p)) as ToolStripButton;

    // If the ToolStripButton control is the empty cell,
    // indicate that the move operation is valid.
    if( item == this.emptyCellButton )
    {
        // Set the drag operation to indicate a valid move.
        dea.Effect = DragDropEffects.Move;
    }
}
' This method defines the DragOver event behavior. 
Protected Overrides Sub OnDragOver(dea As DragEventArgs)
   MyBase.OnDragOver(dea)
   
   ' Get the ToolStripButton control 
   ' at the given mouse position.
   Dim p As New Point(dea.X, dea.Y)
   Dim item As ToolStripButton = CType(Me.GetItemAt(Me.PointToClient(p)), ToolStripButton)
   
   
   ' If the ToolStripButton control is the empty cell,
   ' indicate that the move operation is valid.
     If item Is Me.emptyCellButton Then
         ' Set the drag operation to indicate a valid move.
         dea.Effect = DragDropEffects.Move
     End If
 End Sub

Remarks

The DragEnter event is raised when the user first drags the mouse cursor over the item during a drag-and-drop operation.

The following remarks describe how and when events related to drag-and-drop operations are raised.

The DoDragDrop method determines the item under the current cursor location. It then checks to see if the item is a valid drop target.

If the item is a valid drop target, the GiveFeedback event is raised with the drag-and-drop effect specified. 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:

  • If the user moves out of a window, the DragLeave event is raised.

  • If the mouse enters another item, the DragEnter for that control is raised.

  • If the mouse moves but stays within the same item, the DragOver event is raised.

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.

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. For a list of valid drop effects, see the DragDropEffects enumeration.

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:

  • 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.

  • If the value of DragAction is Cancel, the DragLeave event is raised.

Note

The X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates.

For more information about handling events, see Handling and Raising Events.

Applies to