vincentchang Posted March 10, 2006 Posted March 10, 2006 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 Quote
Leaders snarfblam Posted March 10, 2006 Leaders Posted March 10, 2006 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: // 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); } } Quote [sIGPIC]e[/sIGPIC]
vincentchang Posted March 13, 2006 Author Posted March 13, 2006 Thanks very much, marble_eater. By using delegate, my problem is solved. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.