0

I'm using ApplicationContext to show 2 windows of the same form. This works perfect. But now I have to show an extra dialog form inside both windows where the user can choose some settings.

When I display this dialogform with ShowDialog the focus is only in one window and the other window is frozen until I close the dialogForm. I know this is because showdialog shows the form in modal mode.

How can I avoid this behaviour and let both forms act as independent from each other? Can I do this with ApplicationContext?

Program.cs:

class MyApplicationContext : ApplicationContext
{
    List<MainForm> forms = new List<MainForm>();

    public MyApplicationContext(int numForms)
    {
        for (int i = 0; i < numForms; i++)
        {
            forms.Add(new MainForm(i));
        }
    }
     void MyApplicationContext_FormClosing(object sender, FormClosingEventArgs e)
    {
        Application.Exit();
    }
}

[STAThread]
static void Main(string[] args){
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       MyApplicationContext ac = new MyApplicationContext(2);
       Application.Run(ac);
 }

MainForm.cs:

 public MainForm(int id)
 {
      InitializeComponent();
      this.Invoke(new MethodInvoker(delegate ()
      {
          DialogForm frm = new DialogForm ();
          frm.ShowDialog(this);
      }));
 }
purbsel
  • 307
  • 8
  • 21
  • So how do you want both of your windows to behave actually? Do you want them to be active while dialog is displayed? – Dmitry Rotay May 10 '16 at 09:56
  • I want the showdialog functionality - the mainform where the dialogform is shown should be disabled/inactive. – purbsel May 10 '16 at 10:27
  • I think you gotta check out the top question in the "related" sidebar. Second answer pretty much covers what you need, I guess: http://stackoverflow.com/questions/428494/is-it-possible-to-use-showdialog-without-blocking-all-forms?rq=1 – Dmitry Rotay May 10 '16 at 10:40
  • but doesn't than the ApplicationContext become redundant, because I have to run the Mainforms on two separate threads? – purbsel May 10 '16 at 11:37
  • 1
    If you pick the multithreaded approach, than it might be redundant, yes. But I personally like the second most upvoted answer more, wich involves interops and requires a different approach to dialog form calls. – Dmitry Rotay May 10 '16 at 11:47

0 Answers0