Tutorial :Showing Multiple Instances Of Same Form?


I'm having some serious issues with a WinForm application that I'm working on.
Currently, I'm using Form1.ShowDialog(); to display a form. This code is contained in a background worker that looks for changes in a database. Using Form1.ShowDialog(); only allows 1 form to open at a time, even if there are multiple changes to the database. What I want to have happen is for multiple forms to open at once if there is more than one change in my database.

When I use Form1.Show();, the application blows up. For some reason, the Show() method makes the forms not display properly (all the elements in the form are missing).

Is there anything I can do to make my code work the way I want it to?

Edit: here's a code snippet

//result is a linq result  foreach (var row in result)  {  Form1 Form = new Form1();  Form.ShowDialog();  }  


After a first look, I can tell you this:

  • Showdialog can't work the way you intend: this very method makes the owner inactive until the dialog is closed. In your case, the loop will pause at the first showdialog, then resume when you close the form, opening a new one and so on until the loop is finished.
  • As for the "show" problem, creating empty forms, I need more information. The rest of the code and the exception(s) you're getting.


Two points from the top of my head:

1) To open more then one form , use non modal (modeless) method (i think the show() method). see for example http://msdn.microsoft.com/en-us/library/39wcs2dh.aspx

2) I am not sure you can call UI related method from a non UI thread. You might want to send an event to your UI thread from the worker thread and the UI thread will call the show method

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »