Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Control.AllowDrop-Eigenschaft

 

Veröffentlicht: Oktober 2016

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual bool AllowDrop { get; set; }

Eigenschaftswert

Type: System.Boolean

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

Hinweise für Vererber:

When overriding the P:System.Windows.Forms.Control.AllowDrop property in a derived class, use the base class's P:System.Windows.Forms.Control.AllowDrop property to extend the base implementation. Otherwise, you must provide all the implementation. You are not required to override both the get and setaccessors of the P:System.Windows.Forms.Control.AllowDrop property; you can override only one if needed.

The following code example enables the user to drag an image or image file onto the form, and have it be displayed at the point on it is dropped. The M:System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs) method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting. The E:System.Windows.Forms.Control.DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. The E:System.Windows.Forms.Control.DragDrop event-handling method displays the image on the form, if an T:System.Drawing.Image can be created from the data. Because the P:System.Windows.Forms.DragEventArgs.X and P:System.Windows.Forms.DragEventArgs.Y values are screen coordinates, the example uses the M:System.Windows.Forms.Control.PointToClient(System.Drawing.Point) method to convert them to client coordinates.

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;
   }
}

UIPermission

for unrestricted Clipboard access to set this property to true. Associated enumeration: the AllClipboard value of T:System.Security.Permissions.UIPermissionClipboard

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: