Drag & Drag and ShowDialog problem

vincentchang

Newcomer
Joined
Oct 7, 2005
Messages
6
Hi All,

I just find a problem using drag & drag and ShowDialog problem in Visual Studio 2003.

In my form, I add DragDrop method:

private void Form11_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
using(Form f1 = new Form10())
{
f1.ShowDialog(this);
}
}
}

Then I drag a file from Windows Explorer to the form and Form10 opens as expected. However, when I switch to Windows Explorer, I can't use the Windows Explorer as the focus is on Form10. If I close Form10, then the Windows Explorer can be reached and used again.

Can anyone help me to solve the above problem?

Many Thanks,
Vincent
 
This is probably because the drag & drop source is waiting for the drag & drop operation to be completed. By performing a ShowDialog() in the event handler, which doesn't return control to the caller until the form is hidden, you are tying up the thread. Try something like this, which will show the modal dialog in the application's main UI thread:
C#:
// Invoke function that will show modal form on UI thread
private void MyControl_DragDrop(object sender, DragEventArgs e) {
    this.BeginInvoke(new SimpleVoid(ShowIt));
}

delegate void SimpleVoid(); // Delegate type to use with BeginInvoke

public void ShowIt() { // function to show modal form on UI thread
    using(Form1 newform = new Form1()) {
        newform.ShowDialog(this);
    }
}
 
Back
Top