This documentation is archived and is not being maintained.

Control::AllowDrop Property

Gets or sets a value indicating whether the control can accept data that the user drags onto it.

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

public:
virtual property bool AllowDrop {
	bool get ();
	void set (bool value);
}

Property Value

Type: System::Boolean
true if drag-and-drop operations are allowed in the control; otherwise, false. The default is false.

Notes to Inheritors:

When overriding the AllowDrop property in a derived class, use the base class's AllowDrop property to extend the base implementation. Otherwise, you must provide all the implementation. You are not required to override both the get and set accessors of the 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 OnPaint method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting. The DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. The DragDrop event-handling method displays the image on the form, if an Image can be created from the data. Because the DragEventArgs::X and DragEventArgs::Y values are screen coordinates, the example uses the PointToClient method to convert them to client coordinates.

private:
   Image^ picture;
   Point pictureLocation;

public:
   Form1()
   {

      // Enable drag-and-drop operations and 
      // add handlers for DragEnter and DragDrop. 
      this->AllowDrop = true;
      this->DragDrop += gcnew DragEventHandler( this, &Form1::Form1_DragDrop );
      this->DragEnter += gcnew DragEventHandler( this, &Form1::Form1_DragEnter );
   }

protected:
   virtual void OnPaint( PaintEventArgs^ e ) override
   {

      // If there is an image and it has a location, 
      // paint it when the Form is repainted.
      Form::OnPaint( e );
      if ( this->picture != nullptr && 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. 
         array<String^>^files = (array<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( 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 = dynamic_cast<Image^>(e->Data->GetData( DataFormats::Bitmap ));

            // Set the picture location equal to the drop point. 
            this->pictureLocation = this->PointToClient( Point(e->X,e->Y) );
         }
         catch ( Exception^ ex ) 
         {
            MessageBox::Show( ex->Message );
            return;
         }
      }

      // Force the form to be redrawn with the image. 
      this->Invalidate();
   }

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: