#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 software development and sailing.

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 comment