Control.AllowDrop Propiedad

Definición

Obtiene o establece un valor que indica si el control puede aceptar los datos que el usuario arrastra al mismo.

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

Valor de propiedad

true si están permitidas las operaciones de arrastrar y colocar en el control; en caso contrario, false. De manera predeterminada, es false.

Ejemplos

El ejemplo de código siguiente permite al usuario arrastrar una imagen o un archivo de imagen al formulario y mostrarlo en el punto en el que se coloca. El OnPaint método se invalida para volver a pintar la imagen cada vez que se pinta el formulario; de lo contrario, la imagen solo persistiría hasta que se vuelva a pintar la siguiente. El DragEnter método de control de eventos determina el tipo de datos que se arrastran al formulario y proporciona los comentarios adecuados. El DragDrop método de control de eventos muestra la imagen en el formulario, si se puede crear a Image partir de los datos. Dado que los DragEventArgs.X valores y DragEventArgs.Y son coordenadas de pantalla, en el ejemplo se usa el PointToClient método para convertirlos en coordenadas de cliente.

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

Notas a los desarrolladores de herederos

Al invalidar la AllowDrop propiedad en una clase derivada, utilice la propiedad de AllowDrop la clase base para extender la implementación base. De lo contrario, debe proporcionar toda la implementación. No es necesario invalidar los get descriptores de acceso y set de la AllowDrop propiedad; solo se puede invalidar uno si es necesario.

Se aplica a

Producto Versiones
.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

Consulte también