Adam
Adam

Reputation: 612

Is it possible to drag something from a dialog window to it's parent in C#

Just as an example, say I have a form with a listbox. From this form I open a dialog window using dialog.ShowDialog(this), which has another listbox. Normally, the user would double-click items in the dialog's listbox to add it to the owner form's listbox, then close the dialog when they are done. I want to know if I could enable drag and drop so the user could instead drag an item from the dialog's listbox to the owner form's listbox. From what I can tell, at least on my Windows 7 computer (using .Net Framework 4.0), this is not possible.

An additional feature I'd like, but is not necessary, is if the owner form could be brought in front of the dialog window while user is dragging item over owner form. (This is to enable the user to better see the listbox on the owner form while dragging.)

Upvotes: 0

Views: 333

Answers (2)

Adam
Adam

Reputation: 612

This can be done by having the owner form handle the drag event directly rather than having the child control on the form handle the drag event. Set AllowDrop = true and handle DragDrop and DragOver events for the form. You can use control.ClientRectangle.Contains(control.PointToClient(new Point(e.X, e.Y))) in the form's drag events to determine if the mouse is in the desired control's client region.

You cannot bring the owner window in front of the dialog (and probably shouldn't try), but the user can easily move the dialog out of their way if necessary before beginning the drag.

Note: You may want to add if (!this.CanFocus) e.Effect = DragDropEffects.None; to your form's DragOver event to disable drag events while dialog is being shown, unless the drag is from the dialog window.

Upvotes: 0

ManBearPig
ManBearPig

Reputation: 93

I dont think this is possible, more accurately not advisable. Clarify if I am wrong. What you are trying to do is drag an item from a modal that appears super-imposed on the form ( Instead of double-clicking and bubbling the data back up to the parent). In order to achieve a drag and drop, somehow you will have to, upon clicking to drag, trigger a loss of focus on the modal, and then drop, after which you want to regain focus on the modal. To me this seems like a circuitous way of going about a minor quality of life improvement.

Upvotes: 2

Related Questions