Answered by:
Drag-N-Drop Window Lock

Question
-
In my application, I have two forms, the main form, and a dialog form. When a user drags files onto the main form, it opens the dialog to allow the user to specify certain information about the files. On the dialog I have a list box that displays the names of the files that are being edited. I have this list box designed to also accept drag-n-drop, in which case it should add the new files to the end of the list.
All this works perfectly except for one little detail. When you drag an drop to the main form, it locks the window so that you can not add more files to the dialog from it. You can add more files to the dialog from other windows, just not the one used to drag and drop to the main form. This is an issue as I know my users will complain about it. I have tried created a custom event in which I would open the dialog, hoping that this would allow the dialog to run in a seperate thread thus allowing the main form to release it's handle to the directory window. Didn't work. I have also tried setting the main form as the parent of the dialog and visa-versa, but still no dice. Has anyone ever faced this issue and solved it before? I have scoured the internet and can not find a single solution.
And don't bother asking for code. I gave plenty enough of an explanation to not need it, besides I'm bound by my companies policies to not disclose it. I don't need code, I do my own work, just point me in the right direction.
"Coding your life, Compiling your dreams, and crashing your future."Tuesday, January 31, 2012 7:48 PM
Answers
-
It sound like, for whatever reason, that explorer does not believe that the drop has completed and that is why the explorer window is being less than responsive.
Both Thorsten and I have created sample applications that do not suffer the same symptom, but they are obviously not doing exactly what your code is doing.
This is why we ask for code, so that we can replicate / analyze the problem. I understand an employer not wanting to release production code onto the internet; it would be irresponsible.
The sample applications that Thorsten and I put together probably took us less than five minutes to build and test. While releasing production code into the wild would be wrong, I am sure that you could gen up a sample application that demonstrates your problem without violating your employer's rules.
How do you think that everyone else does it?
I am not trying to lecture here, just making a point.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.- Edited by TSoftware-Old Tuesday, January 31, 2012 9:28 PM
- Marked as answer by Magius96 Monday, February 27, 2012 12:33 AM
Tuesday, January 31, 2012 9:26 PM
All replies
-
Are you showing the dialog using Form.ShowDialog(), by chance? This shows the dialog modally, so you can't interact with the main form while it's open. Instead, you want to show the dialog modelessly using Form.Show().Tuesday, January 31, 2012 8:01 PM
-
I am using Form.Show().
"Coding your life, Compiling your dreams, and crashing your future."Tuesday, January 31, 2012 8:05 PM -
I re-read your problem description. So the window that you are dragging from (i.e., Explorer) is hanging, and not the main form of your application? If that's the case, then I'm guessing that you are not allowing the drag operation to complete. So the source (Explorer) window still thinks it's in the middle of a drag operation, and its UI remains locked. If I had to guess, I'd say that you are showing the dialog in the main form's DragDrop event handler, and for some reason execution never leaves that handler. (The drag operation won't be considered finished until the DragDrop event completes on the drag target window.) But I can't say for sure without seeing code that reproduces the problem. I will try looking into this some more.
Hope this helps.
Tuesday, January 31, 2012 8:46 PM -
Well, I know you don't want code, but I implemented, based upon your description and had no issues. The initial drop of files opened the second form and populated the list box and subsequent drops added to the list box.:
public partial class Form1 : Form { private Form2 m_Form2; public Form1() { InitializeComponent(); m_Form2 = null; AllowDrop = true; } private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) { string[] l_fileSystemObjects = (string[])(e.Data.GetData(DataFormats.FileDrop, false)); foreach (string l_fileSystemObject in l_fileSystemObjects) { if (m_Form2 == null) { m_Form2 = new Form2(); m_Form2.Show(); } m_Form2.listBox1.Items.Add(l_fileSystemObject); } } } }
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.- Proposed as answer by TSoftware-Old Thursday, February 2, 2012 4:49 AM
Tuesday, January 31, 2012 8:49 PM -
Just to clarify, both of your drops were from the same directory window, right? And your second drop was onto the second form and was handled there, right?
"Coding your life, Compiling your dreams, and crashing your future."- Edited by Magius96 Tuesday, January 31, 2012 8:56 PM
Tuesday, January 31, 2012 8:53 PM -
You didn't specify, so yes, both were, but I just tested with different explorer windows and different directories....still no issues.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.Tuesday, January 31, 2012 8:55 PM -
Just to clarify, both of your drops were from the same directory window, right? And your second drop was onto the second form and was handled there, right?
"Coding your life, Compiling your dreams, and crashing your future."
Hi,
should it be that way? Dropping to the main form, open the dialog-form and then dropping more files to what? The main form or the dialog?
Tuesday, January 31, 2012 9:06 PM -
The code that I tested with, I did several drops from different windows, different directories and a variety sequences between the main form and the dialog.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.Tuesday, January 31, 2012 9:08 PM -
Hi TSoftware,
I also tested without any issues...
That's the code I used:
public partial class Form1 : Form { ListBox l = new ListBox(); //delegate void DoIt(string[] s); Form _dlg = null; public Form1() { InitializeComponent(); this.AllowDrop = true; this.DragOver += new DragEventHandler(Form1_DragOver); this.DragDrop += new DragEventHandler(Form1_DragDrop); } void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { //this.BeginInvoke(new DoIt(DoTheThing), e.Data.GetData(DataFormats.FileDrop)); string[] s = (string[])e.Data.GetData(DataFormats.FileDrop); if (_dlg == null || _dlg.IsDisposed) { _dlg = new Form(); _dlg.AllowDrop = true; _dlg.DragOver += new DragEventHandler(Form1_DragOver); _dlg.DragDrop += new DragEventHandler(_dlg_DragDrop); l = new ListBox(); l.Dock = DockStyle.Fill; _dlg.Controls.Add(l); } if (s.Length > 0) { for (int i = 0; i < s.Length; i++) l.Items.Add(s[i]); _dlg.Show(); this.Text = s[0]; } } } void _dlg_DragDrop(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] s = (string[])e.Data.GetData(DataFormats.FileDrop); for (int i = 0; i < s.Length; i++) l.Items.Add(s[i]); } } //void DoTheThing(string[] s) //{ // if (_dlg == null || _dlg.IsDisposed) // { // _dlg = new Form(); // _dlg.AllowDrop = true; // _dlg.DragOver += new DragEventHandler(Form1_DragOver); // _dlg.DragDrop += new DragEventHandler(_dlg_DragDrop); // l = new ListBox(); // l.Dock = DockStyle.Fill; // _dlg.Controls.Add(l); // } // if (s.Length > 0) // { // for (int i = 0; i < s.Length; i++) // l.Items.Add(s[i]); // _dlg.Show(); // this.Text = s[0]; // } //} void Form1_DragOver(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; else e.Effect = DragDropEffects.None; } }
#####################
@OP: Could you provide some code that illustrates your issue? I cannot reproduce...Regards,
Thorsten
- Edited by Thorsten Gudera Tuesday, January 31, 2012 9:23 PM
- Proposed as answer by TSoftware-Old Thursday, February 2, 2012 4:49 AM
Tuesday, January 31, 2012 9:12 PM -
Hi,
should it be that way? Dropping to the main form, open the dialog-form and then dropping more files to what? The main form or the dialog?
Here's what is desired:
One Coded Application, One Explorer window, One Directory, Two Forms, Form A and Form B
Form A is open. Drag files from explorer window to Form A. Form B opens and displays list box with filenames. Drag files from explorer window to Form B. Filenames added to list box.
Here's what currently happens:
One coded application, One explorer window, one directory, two forms, Form A and Form B
Form A is open. Drag files from explorer window to Form A. Form B opens and displays list box with filenames. Explorer window is locked, can't even select files.
Every other drag and drop scenario works, except this one...this is the one i'm working on.
"Coding your life, Compiling your dreams, and crashing your future."- Edited by Magius96 Tuesday, January 31, 2012 9:20 PM
Tuesday, January 31, 2012 9:19 PM -
Ok,
"Explorer window is locked, can't even select files." This is the thing, I cannot reproduce... See my or TSoftware's code, it does the things you specified.
I know, that you can lock the explorer-window when doing long tasks with the files in the DragDrop-Method... A dragdrop+ or link-sign is then displayed in the explorer at the cursor as long as the operation takes.... This could be (usually) prevented by invoking the long tasks from the dragdrop-method
- Edited by Thorsten Gudera Tuesday, January 31, 2012 9:28 PM
Tuesday, January 31, 2012 9:25 PM -
It sound like, for whatever reason, that explorer does not believe that the drop has completed and that is why the explorer window is being less than responsive.
Both Thorsten and I have created sample applications that do not suffer the same symptom, but they are obviously not doing exactly what your code is doing.
This is why we ask for code, so that we can replicate / analyze the problem. I understand an employer not wanting to release production code onto the internet; it would be irresponsible.
The sample applications that Thorsten and I put together probably took us less than five minutes to build and test. While releasing production code into the wild would be wrong, I am sure that you could gen up a sample application that demonstrates your problem without violating your employer's rules.
How do you think that everyone else does it?
I am not trying to lecture here, just making a point.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.- Edited by TSoftware-Old Tuesday, January 31, 2012 9:28 PM
- Marked as answer by Magius96 Monday, February 27, 2012 12:33 AM
Tuesday, January 31, 2012 9:26 PM -
Hi Magius96,Have you tried Thorsten and TSoftware’s code, do it works on your machine?Could you please delete the core code and write an example which can reproduce this issue? I can’t get enough information to reproduce the issue without an example and I think it will make it clear when you delete other codes and you might find the reason when you create the code example. Thanks.Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Friday, February 3, 2012 10:25 AM