DragEventArgs.Data Eigenschaft

Definition

Ruft das IDataObject ab, das die diesem Ereignis zugeordneten Daten enthält.

public:
 property System::Windows::Forms::IDataObject ^ Data { System::Windows::Forms::IDataObject ^ get(); };
public System.Windows.Forms.IDataObject Data { get; }
public System.Windows.Forms.IDataObject? Data { get; }
member this.Data : System.Windows.Forms.IDataObject
Public ReadOnly Property Data As IDataObject

Eigenschaftswert

Die diesem Ereignis zugeordneten Daten.

Beispiele

Im folgenden Beispiel wird ein Drag-and-Drop-Vorgang zwischen zwei ListBox Steuerelementen veranschaulicht. Im Beispiel wird die DoDragDrop -Methode aufgerufen, wenn die Ziehaktion gestartet wird. Die Ziehaktion wird gestartet, wenn sich die Maus während MouseDown des Ereignisses mehr als SystemInformation.DragSize von der Mausposition bewegt hat. Die IndexFromPoint -Methode wird verwendet, um den Index des Elements zu bestimmen, das während des Ereignisses MouseDown gezogen werden soll.

Das Beispiel veranschaulicht auch die Verwendung benutzerdefinierter Cursor für den Drag-and-Drop-Vorgang. Im Beispiel wird davon ausgegangen, dass zwei Cursordateien und 3dwarro.cur3dwno.curim Anwendungsverzeichnis für die benutzerdefinierten Cursor drag und no drop vorhanden sind. Die benutzerdefinierten Cursor werden verwendet, wenn die UseCustomCursorsCheckCheckBox aktiviert ist. Die benutzerdefinierten Cursor werden im GiveFeedback Ereignishandler festgelegt.

Der Tastaturzustand wird im DragOver Ereignishandler für die rechte ListBoxausgewertet, um zu bestimmen, welcher Ziehvorgang auf der Grundlage des Zustands der UMSCHALT-, STRG-, ALT- oder STRG+ALT-TASTEN erfolgt. Die Position in der, an der ListBox der Abbruch erfolgen würde, wird auch während des Ereignisses DragOver bestimmt. Wenn die zu löschenden Daten keine Stringsind, wird auf DragEventArgs.EffectDragDropEffects.Nonefestgelegt. Schließlich wird die status des Drops im DropLocationLabelLabelangezeigt.

Die für die rechte ListBox Seite zu löschenden Daten werden im DragDrop -Ereignishandler bestimmt, und der String Wert wird an der entsprechenden Stelle im ListBoxhinzugefügt. Wenn der Ziehvorgang außerhalb der Grenzen des Formulars verschoben wird, wird der Drag-and-Drop-Vorgang im QueryContinueDrag Ereignishandler abgebrochen.

Dieser Codeauszug veranschaulicht die Verwendung der DragEventArgs -Klasse. DoDragDrop Das vollständige Codebeispiel finden Sie in der -Methode.

void ListDragTarget_DragOver( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ e )
{
   // Determine whether string data exists in the drop data. If not, then
   // the drop effect reflects that the drop cannot occur.
   if (  !e->Data->GetDataPresent( System::String::typeid ) )
   {
      e->Effect = DragDropEffects::None;
      DropLocationLabel->Text = "None - no string data.";
      return;
   }

   // Set the effect based upon the KeyState.
   if ( (e->KeyState & (8 + 32)) == (8 + 32) && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // KeyState 8 + 32 = CTRL + ALT
      // Link drag-and-drop effect.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 32) == 32 && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // ALT KeyState for link.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 4) == 4 && ((e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move) )
   {
      // SHIFT KeyState for move.
      e->Effect = DragDropEffects::Move;
   }
   else
   if ( (e->KeyState & 8) == 8 && ((e->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) )
   {
      // CTRL KeyState for copy.
      e->Effect = DragDropEffects::Copy;
   }
   else
   if ( (e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move )
   {
      // By default, the drop action should be move, if allowed.
      e->Effect = DragDropEffects::Move;
   }
   else
            e->Effect = DragDropEffects::None;





   
   // Get the index of the item the mouse is below.
   // The mouse locations are relative to the screen, so they must be
   // converted to client coordinates.
   indexOfItemUnderMouseToDrop = ListDragTarget->IndexFromPoint( ListDragTarget->PointToClient( Point(e->X,e->Y) ) );
   
   // Updates the label text.
   if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches )
   {
      DropLocationLabel->Text = String::Concat( "Drops before item # ", (indexOfItemUnderMouseToDrop + 1) );
   }
   else
            DropLocationLabel->Text = "Drops at the end.";
}
private void ListDragTarget_DragOver(object sender, DragEventArgs e)
{
    // Determine whether string data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.
    if (!e.Data.GetDataPresent(typeof(System.String)))
    {
        e.Effect = DragDropEffects.None;
        DropLocationLabel.Text = "None - no string data.";
        return;
    }

    // Set the effect based upon the KeyState.
    if ((e.KeyState & (8 + 32)) == (8 + 32) &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // KeyState 8 + 32 = CTRL + ALT

        // Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 32) == 32 &&
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link)
    {
        // ALT KeyState for link.
        e.Effect = DragDropEffects.Link;
    }
    else if ((e.KeyState & 4) == 4 &&
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move;
    }
    else if ((e.KeyState & 8) == 8 &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        // CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy;
    }
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }

    // Get the index of the item the mouse is below. 

    // The mouse locations are relative to the screen, so they must be 
    // converted to client coordinates.

    indexOfItemUnderMouseToDrop =
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y)));

    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
    {
        DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
    }
    else
    {
        DropLocationLabel.Text = "Drops at the end.";
    }
}
Private Sub ListDragTarget_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragOver
    ' Determine whether string data exists in the drop data. If not, then
    ' the drop effect reflects that the drop cannot occur.
    If Not (e.Data.GetDataPresent(GetType(System.String))) Then

        e.Effect = DragDropEffects.None
        DropLocationLabel.Text = "None - no string data."
        Return
    End If

    ' Set the effect based upon the KeyState.
    If ((e.KeyState And (8 + 32)) = (8 + 32) And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then
        ' KeyState 8 + 32 = CTRL + ALT

        ' Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 32) = 32 And
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then

        ' ALT KeyState for link.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 4) = 4 And
        (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move

    ElseIf ((e.KeyState And 8) = 8 And
        (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then

        ' CTRL KeyState for copy.
        e.Effect = DragDropEffects.Copy

    ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move

    Else
        e.Effect = DragDropEffects.None
    End If

    ' Gets the index of the item the mouse is below. 

    ' The mouse locations are relative to the screen, so they must be 
    ' converted to client coordinates.

    indexOfItemUnderMouseToDrop =
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(New Point(e.X, e.Y)))

    ' Updates the label text.
    If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then
        DropLocationLabel.Text = "Drops before item #" & (indexOfItemUnderMouseToDrop + 1)
    Else
        DropLocationLabel.Text = "Drops at the end."
    End If

End Sub

Hinweise

Im Text des Ereignishandlers können Sie mit der GetDataPresent -Methode bestimmen, ob die Daten den Formatanforderungen des Steuerelements entsprechen, auf das die Daten gezogen werden. Sie können auch angeben, ob Sie versuchen möchten, die Daten so zu konvertieren, dass sie Ihren Formatanforderungen entsprechen. Wenn die gezogenen Daten Ihre Formatanforderungen erfüllen, verwenden Sie die GetData -Methode, um die Daten abzurufen.

Gilt für:

Weitere Informationen