Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BIG drag'n drop problem!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hi,I have a big drag and drop problem and I really can't figure how to solve it.
I have a dblookuplist box displaying the 'name' field of a dataset. The OnMouseDown event is: listbox1.begindrag(false). so that the user can drag a name out of the box. This was working fine until I added a OnDblClick event: form2.ShowModal.
The form 2 displays correctly, the user can clicks on OK. Now we get into trouble. The lisbox thinks we are in a drag operation since the onMouseDown event seemed to be triggered when we double click. After closing the modal form, the onstartdrag event is triggered when the user moves the mouse a little bit...
It's hard to explain. It seems that the modal form takes control of the program and then when you press OK, the listbox think you are dragging the object...Hope you understand.
Thanks
Dave [sig][/sig]
 
Maybe you need to set form's KeyPreview to true.

Hope this can help you!
 
Using the OnMouseDown event to start the dragging operation is incorrect, since any kind of mouse click could trigger the drag operation. You cannot really identify the mouse operation as drag-and-drop until after the user, still holding the mouse button down, moves the mouse a minimum distance.

Most programmers say the minimum distance is 5 pixels. In otherwords, the user must move the mouse at least 5 pixels away from where the mouse button was originally pressed before drag-and-drop mode begins.

To properly implement drag-and-drop, you could write code in your OnMouseMove event to track how far the mouse has moved. When it goes over 5 pixels away from the original mouse-down location, then you can call BeginDrag.

OR, you can make your life really easy and just set the TDBLookupListBox.DragMode property to dmAutomatic. Doing this automatically enforces the 5 pixel rule, and allows you to eliminate your OnMouseDown/OnMouseMove logic. With your OnMouseDown logic gone, you'll no longer have the Double-Click problem.

One other note:
If you use a Double-click event to close a form (i.e. selecting something and automatically closing the form), and that form was overlaying another form having a control immediately behind the place that was double-clicked...

the double-click will also register a MouseDown event on the control sitting on the overlayed form immediately behind the place double-clicked! For this reason, ALWAYS be careful when using OnMouseDown events.
 
Onealm's response is not entirely correct.

A:
You can identify a dblclick even inside an OnMouseDown event since the OnDblClick even occurs before the OnMouseDown does. You could simply, inside your OnDblClick event set TAG := 999, then inside the OnMouseDown do:

if TAG <> 999 then
{ Do drag'n'drop };
TAG := 0;

B:
Using OnMouseMove to calculate the distance the object has been dragged to is a bit difficult, since it will only be triggered once the mouse cursor is over your object. To calculate drag distance properly you would have to hook into windows cursor change messages.

C:
Changing the DragMode property to dmAutomatic will not allow the control to receive mouse clicks. As far as I know it does not enforce the 5 points rule but automatically begins a drag'n'drop proccess immediatly. To enforce the 5 pixels limit you would have to use BeginDrag(false), as gretzteam uses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top