#703 – Indicating Formats that Are Allowed to be Dropped

A control becomes a drop target by setting its AllowDrop property to true and handling its Drop event, where you retrieve the dragged data.  You could also verify in the Drop event whether the data being dropped is a format that the control can handle.  But at that point, the data has already been dropped.

The preferred method for a control to indicate which types of data it can handle is to handle the DragEnter and DragOver events and set the DragEventArgs.Effects property to indicate that the operation is not allowed.  This will result in the mouse cursor indicating that the drop operation is not allowed.  The Drop event will also not fire.

        private void Image_DragEnter(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;

            e.Handled = true;
        }

        private void Image_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;

            e.Handled = true;
        }

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #703 – Indicating Formats that Are Allowed to be Dropped

  1. Pingback: Dew Drop – December 3, 2012 (#1,454) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: