My Potential My Passion

You are never given a wish, without also being given the power to make it come true - Richard Bach

Thursday, October 21, 2004

Drag Drop and MouseUp mystery solved

The DoDragDrop method, when called for a control, eats up the mouseup event till the drag drop operation doesn't complete.

I was facing this problem for a scenario where, I wanted to initiate the DoDragDrop operation on mousedown event on a custom control dervied from Component One Grid and wanted to do some selection operation on MouseUp event.

When I coded the DoDragDrop action in MouseDown event it would simply eat up the mouseup event on which I had coded something else. As a result, I had to click twice on my control for the selection to happen.

After some bit of R&D I finally got through this situation. Here's what I did:

1. Maintained a boolean flag to check if mousedown or not - call it _IsMouseDown
2. In the MouseDown event I set this flag to true.
3. In the MouseUp event I set the flag to false
4. I also handle the MouseMove event. I did some bit of extra coding in here. Here's what I do in MouseMove:
a. First I check if the MouseMove is about to result in the cursor going outside the bounds of my control. If Yes then I set a flag _ShouldDragDrop to true else it remains false.
b. To check if the bounds are going outside my control or not, I convert the mouse co-ordinates that I got from the MouseEventArgs to Screen co-ordinates using the PointToScreen method of the parent control. And then I again convert it to client co-ordinates as per the Parent control of my control. (Note: The Parent control is a container for the Component One Grid control and the grid control's dockstyle is set to fill)
c. Then with respect to my parent control's topleft and bottom right corners if the mouse cursor is inside thse bounds then do not start the drag drop operation else when the cursor goes outside these bounds, start the DoDragDrop action.

Now, since the DoDragDrop operation doesnt happen till the cursor goes outside the bounds of my control, the MouseUp event on my control doesn't get eaten up and at the same time when the control is going outside my control the dragdrop operation automatically starts. :)