none
Disable a Form

    Question

  • I'm looking to disable a form without setting Enabled to false. What I'm looking for is basically how to do what ShowDialog does to the parent form. I tried using SendMessage to send WM_ACTIVATE with the WA_INACTIVE flag set but it doesn't do anything. Anybody know what message I have to send?

    [Edit] I'm really looking to do exactly what ShowDialog does, so disable the form, not letting the user bring it to front, etc...

    Wednesday, April 07, 2010 8:04 PM

Answers

  • Found an easy way to have multiple "main" forms. ShowDialog only takes over one of them and closing any of them closes them all.

    private static volatile bool closing = false;
    private static object closingLock = new object();
    
    private static void RunForm(Form form)
    {
        form.FormClosing += new FormClosingEventHandler(mainForms_FormClosing);
        mainForms.Add(form);
    
        Application.Run(form);
    }
    
    private static void ThreadRunForm(Form form)
    {
        Thread thread = new Thread(new ParameterizedThreadStart(RunForm));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start(form);
    }
    
    private static void RunForm(object form)
    {
        RunForm((Form)form);
    }
    
    private static void mainForms_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!closing)
            lock (closingLock)
                if (!closing)
                {
                    closing = true;
    
                    Form closingForm = sender as Form;
    
                    foreach (Form mainForm in mainForms)
                        if (mainForm != closingForm)
                            mainForm.Invoke(new MethodInvoker(delegate
                            {
                                mainForm.Close();
                            }));
                }
    }
    [Edit] Fixed FormClosing to make it thread safe, don't know if the lock is actually necessary but better safe than sorry.
    Thursday, April 08, 2010 2:03 PM

All replies

  • ShowDialog a hidden form.
    Wednesday, April 07, 2010 8:51 PM
  • No the problem is that ShowDialog takes over the entire application. I want to create my own ShowDialog which only disables the parent. This way I can have two base forms and only disable one.
    Thursday, April 08, 2010 1:22 PM
  • Found an easy way to have multiple "main" forms. ShowDialog only takes over one of them and closing any of them closes them all.

    private static volatile bool closing = false;
    private static object closingLock = new object();
    
    private static void RunForm(Form form)
    {
        form.FormClosing += new FormClosingEventHandler(mainForms_FormClosing);
        mainForms.Add(form);
    
        Application.Run(form);
    }
    
    private static void ThreadRunForm(Form form)
    {
        Thread thread = new Thread(new ParameterizedThreadStart(RunForm));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start(form);
    }
    
    private static void RunForm(object form)
    {
        RunForm((Form)form);
    }
    
    private static void mainForms_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!closing)
            lock (closingLock)
                if (!closing)
                {
                    closing = true;
    
                    Form closingForm = sender as Form;
    
                    foreach (Form mainForm in mainForms)
                        if (mainForm != closingForm)
                            mainForm.Invoke(new MethodInvoker(delegate
                            {
                                mainForm.Close();
                            }));
                }
    }
    [Edit] Fixed FormClosing to make it thread safe, don't know if the lock is actually necessary but better safe than sorry.
    Thursday, April 08, 2010 2:03 PM