Control.AllowDrop (Propiedad)
Actualización: noviembre 2007
Obtiene o establece un valor que indica si el control puede aceptar los datos que el usuario arrastra al mismo.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
/** @property */ public boolean get_AllowDrop() /** @property */ public void set_AllowDrop(boolean value)
public function get AllowDrop () : boolean public function set AllowDrop (value : boolean)
Valor de propiedad
Tipo: System.BooleanEs true si se permite realizar operaciones de arrastrar y colocar en el control; en caso contrario, es false. El valor predeterminado es false.
Cuando se reemplaza la propiedad AllowDrop en una clase derivada, hay que utilizar la propiedad AllowDrop de la clase base para extender la implementación base. Si no, deberá proporcionarse toda la implementación. No es necesario reemplazar ambos descriptores de acceso get y set de la propiedad AllowDrop; se puede reemplazar sólo uno, si es necesario.
En el ejemplo siguiente de código se muestra cómo permitir al usuario arrastrar una imagen o un archivo de imagen al formulario, y mostrarlo en el lugar donde se coloque. El método OnPaint se reemplaza para volver a dibujar la imagen cada vez que se dibuje el formulario; en caso contrario, la imagen sólo se conserva hasta la siguiente operación de dibujo. El método de control de eventos DragEnter determina el tipo de datos que se arrastra al formulario y proporciona la información apropiada. El método de control de eventos DragDrop muestra la imagen en el formulario si se puede crear Image a partir de los datos. Como los valores de las propiedades DragEventArgs.X y DragEventArgs.Y son coordenadas de pantalla, se utiliza en el ejemplo el método PointToClient para convertirlas a coordenadas de cliente.
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; } }
private Image picture;
private Point pictureLocation;
public Form1()
{
// Enable drag-and-drop operations and
// add handlers for DragEnter and DragDrop.
this.set_AllowDrop(true);
this.add_DragDrop(new DragEventHandler(this.Form1_DragDrop));
this.add_DragEnter(new DragEventHandler(this.Form1_DragEnter));
} //Form1
protected void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
super.OnPaint(e);
if (this.picture != null && !this.pictureLocation.Equals(Point.Empty)) {
e.get_Graphics().DrawImage(this.picture, this.pictureLocation);
}
} //OnPaint
private void Form1_DragDrop(Object sender, DragEventArgs e)
{
// Handle FileDrop data.
if (e.get_Data().GetDataPresent(DataFormats.FileDrop)) {
// Assign the file names to a string array, in
// case the user has selected multiple files.
String files[] = (String[])(e.get_Data().GetData(
DataFormats.FileDrop));
try {
// Assign the first image to the picture variable.
this.picture = Image.FromFile(files.toString());
// Set the picture location equal to the drop point.
this.pictureLocation = this.PointToClient(new Point(e.get_X(),
e.get_Y()));
}
catch (System.Exception ex) {
MessageBox.Show(ex.get_Message());
return;
}
}
// Handle Bitmap data.
if (e.get_Data().GetDataPresent(DataFormats.Bitmap)) {
try {
// Create an Image and assign it to the picture variable.
this.picture = (Image)e.get_Data().GetData(DataFormats.Bitmap);
// Set the picture location equal to the drop point.
this.pictureLocation = this.PointToClient(new Point(e.get_X(),
e.get_Y()));
}
catch (System.Exception ex) {
MessageBox.Show(ex.get_Message());
return;
}
}
// Force the form to be redrawn with the image.
this.Invalidate();
} //Form1_DragDrop
private void Form1_DragEnter(Object sender, DragEventArgs e)
{
// If the data is a file or a bitmap, display the copy cursor.
if (e.get_Data().GetDataPresent(DataFormats.Bitmap) || e.get_Data().
GetDataPresent(DataFormats.FileDrop)) {
e.set_Effect(DragDropEffects.Copy);
}
else {
e.set_Effect(DragDropEffects.None);
}
} //Form1_DragEnter
- UIPermission
para obtener acceso no restringido al Portapapeles con el fin de establecer esta propiedad en true. Enumeración asociada: el valor AllClipboard de System.Security.Permissions.UIPermissionClipboard.
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.