Auf Englisch lesen

Freigeben über


Control.AllowDrop Eigenschaft

Definition

Ruft einen Wert ab, der angibt, ob das Steuerelement Daten annehmen kann, die vom Benutzer darauf gezogen wurden, oder legt diesen fest.

C#
public virtual bool AllowDrop { get; set; }

Eigenschaftswert

true, wenn Drag & Drop-Vorgänge für das Steuerelement zulässig sind, andernfalls false. Der Standardwert ist false.

Beispiele

Im folgenden Codebeispiel kann der Benutzer ein Bild oder eine Bilddatei auf das Formular ziehen und an der Stelle anzeigen lassen, an der es gelöscht wird. Die OnPaint -Methode wird überschrieben, um das Bild jedes Mal neu zu streichen, wenn das Formular gezeichnet wird. Andernfalls wird das Bild nur bis zur nächsten Neulackierung beibehalten. Die DragEnter Ereignisbehandlungsmethode bestimmt den Typ der Daten, die in das Formular gezogen werden, und gibt das entsprechende Feedback. Die DragDrop Ereignisbehandlungsmethode zeigt das Bild auf dem Formular an, wenn aus den Daten eine Image erstellt werden kann. Da die DragEventArgs.X Werte und DragEventArgs.Y Bildschirmkoordinaten sind, wird im Beispiel die PointToClient -Methode verwendet, um sie in Clientkoordinaten zu konvertieren.

C#
private Image picture;
private Point pictureLocation;

public Form1()
{
   // Enable drag-and-drop operations and 
   // add handlers for DragEnter and DragDrop.
   this.AllowDrop = true;
   this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
   this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
}

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
   // Handle FileDrop data.
   if(e.Data.GetDataPresent(DataFormats.FileDrop) )
   {
      // Assign the file names to a string array, in 
      // case the user has selected multiple files.
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      try
      {
         // Assign the first image to the picture variable.
         this.picture = Image.FromFile(files[0]);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }

   // Handle Bitmap data.
   if(e.Data.GetDataPresent(DataFormats.Bitmap) )
   {
      try
      {
         // Create an Image and assign it to the picture variable.
         this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }
   // Force the form to be redrawn with the image.
   this.Invalidate();
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is a file or a bitmap, display the copy cursor.
   if (e.Data.GetDataPresent(DataFormats.Bitmap) || 
      e.Data.GetDataPresent(DataFormats.FileDrop) ) 
   {
      e.Effect = DragDropEffects.Copy;
   }
   else
   {
      e.Effect = DragDropEffects.None;
   }
}

Hinweise für Vererber

Verwenden Sie beim Überschreiben der AllowDrop -Eigenschaft in einer abgeleiteten Klasse die -Eigenschaft der AllowDrop Basisklasse, um die Basisimplementierung zu erweitern. Andernfalls müssen Sie die gesamte Implementierung bereitstellen. Sie müssen nicht sowohl die Accessoren als set auch die get Accessoren der AllowDrop -Eigenschaft außer Kraft setzen. Sie können bei Bedarf nur einen überschreiben.

Gilt für:

Produkt Versionen
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Weitere Informationen