views:

619

answers:

1

I've written a custom control (an immediate child of Forms.Control) in C++/CLI that will offload most of its processing and all of its painting to a separate legacy-ish MFC control. I'm wrapping this control in a C# application. I need to be able to drag items onto the C++/CLI control from a UserControl on the same form in the application.

For some reason, my custom control isn't receiving any of the drag/drop events in spite of my setting AllowDrop to true. (I've verified that AllowDrop is true at runtime.) I never get an opportunity to examine/manipulate the DragEventArgs because the callbacks are never called.

I've verified that drag and drop otherwise functions normally. If I replace the custom control with a Panel, for example, I get the dragdrop callbacks just fine.

Is there something additional that must be implemented in a Control to support dragdrop callbacks? Is there something subtle that must be dealt with for the system to recognize that a control is being hovered over and should be the target of a dragdrop operation?

Notes:

The MFC control that will do the painting is not yet available to me, so I'm just painting a simple gradient background (via OnPaintBackground) that I'm trying to paint to. I'm also painting a smaller gradient rectangle in OnPaint.

I have tried setting AllowDrop = true both before the custom control's handle is created (via designer/constructor code) and after the handle is created (via OnHandleCreated override). No difference in behavior.

The drag cursor never changes from the default "no drag available" cursor over the custom control.

I'm calling this.DoDragDrop() from an event handler for a MouseDown event on a subcontrol of the UserControl.

DragDrop seems to work ok with another custom control I created in C# in the same assembly as the user control. It captures the callbacks just fine. Only my C++/CLI control isn't seeing them. The initializecomponent goo is irrelevant. I can comment it all out with no change.

+2  A: 

I figured out my mistake, so I'll answer it here in case someone else makes a similar mistake. It looks like I did hint at the answer in my question, though I didn't realize it at the time.

In my question, I stated that

I have tried setting AllowDrop = true both before the custom control's handle is created (via designer/constructor code) and after the handle is created (via OnHandleCreated override). No difference in behavior.

This is actually where the breakdown occurred. I failed to read the OnHandleCreated documentation that specifically states I needed to remember to call the base class' implementation of OnHandleCreated.

Reflector shows that Control::OnHandleCreated is responsible for setting up Control's internal event notification structure. I never called Control::OnHandleCreated, so the internal event notification structure was never getting properly built up. This eventing structure (reflector again shows) is responsible for firing OnDragOver, etc, so the missing eventing structure resulted in my missing events.

Answer:

Be sure that you call the base class implementation of OnHandleCreated when you override it!

Greg D