Breyta

Deila með


Control.QueryContinueDrag Event

Definition

Occurs during a drag-and-drop operation and enables the drag source to determine whether the drag-and-drop operation should be canceled.

public:
 event System::Windows::Forms::QueryContinueDragEventHandler ^ QueryContinueDrag;
public event System.Windows.Forms.QueryContinueDragEventHandler QueryContinueDrag;
public event System.Windows.Forms.QueryContinueDragEventHandler? QueryContinueDrag;
member this.QueryContinueDrag : System.Windows.Forms.QueryContinueDragEventHandler 
Public Custom Event QueryContinueDrag As QueryContinueDragEventHandler 

Event Type

Examples

This code excerpt demonstrates using the QueryContinueDrag event to cancel the drag-and-drop operation if the drag operation moves outside the bounds of the form. See the DoDragDrop method for the complete code example.

void ListDragSource_QueryContinueDrag( Object^ sender, System::Windows::Forms::QueryContinueDragEventArgs^ e )
{
   // Cancel the drag if the mouse moves off the form.
   ListBox^ lb = dynamic_cast<ListBox^>(sender);
   if ( lb != nullptr )
   {
      Form^ f = lb->FindForm();

      // Cancel the drag if the mouse moves off the form. The screenOffset
      // takes into account any desktop bands that may be at the top or left
      // side of the screen.
      if ( ((Control::MousePosition.X - screenOffset.X) < f->DesktopBounds.Left) || ((Control::MousePosition.X - screenOffset.X) > f->DesktopBounds.Right) || ((Control::MousePosition.Y - screenOffset.Y) < f->DesktopBounds.Top) || ((Control::MousePosition.Y - screenOffset.Y) > f->DesktopBounds.Bottom) )
      {
         e->Action = DragAction::Cancel;
      }
   }
}
private void ListDragSource_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
    // Cancel the drag if the mouse moves off the form.
    ListBox lb = sender as ListBox;

    if (lb != null)
    {
        Form f = lb.FindForm();

        // Cancel the drag if the mouse moves off the form. The screenOffset
        // takes into account any desktop bands that may be at the top or left
        // side of the screen.
        if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) ||
            ((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) ||
            ((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) ||
            ((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom))
        {
            e.Action = DragAction.Cancel;
        }
    }
}
Private Sub ListDragSource_QueryContinueDrag(ByVal sender As Object, ByVal e As QueryContinueDragEventArgs) Handles ListDragSource.QueryContinueDrag
    ' Cancel the drag if the mouse moves off the form.
    Dim lb As ListBox = CType(sender, ListBox)

    If (lb IsNot Nothing) Then

        Dim f As Form = lb.FindForm()

        ' Cancel the drag if the mouse moves off the form. The screenOffset
        ' takes into account any desktop bands that may be at the top or left
        ' side of the screen.
        If (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) Or
            ((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) Or
            ((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) Or
            ((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) Then

            e.Action = DragAction.Cancel
        End If
    End If
End Sub

Remarks

The QueryContinueDrag event is raised when there is a change in the keyboard or mouse button state during a drag-and-drop operation. The QueryContinueDrag event enables the drag source to determine whether the drag-and-drop operation should be canceled.

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

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

If the control 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.

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

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

  • If the mouse moves but stays within the same control, 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.

    Note

    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.

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

By default, the QueryContinueDrag event sets Action to Cancel in DragAction if the ESC key was pressed and sets Action to Drop in DragAction if the left, middle, or right mouse button is pressed.

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

Applies to

See also