Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an event thrown when a user tries to click off a modal dialog created with Form.ShowDialog()?

TL;DR

While a .ShowDialog() modal dialog is open and the user clicks on the original form, the dialog's title bar flashes. Is that event accessible via the Windows.Forms API, or by any other means?


Details

This is a standard C# 6 Windows Forms project with a parent form and a dialog window. The parent form has a single button that opens the dialog:

using System;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (var dialog = new Dialog())
        {
            Console.WriteLine("Dialog starting.");

            dialog.ShowDialog(this);

            Console.WriteLine("Dialog done.");
        }
    }
}

The Dialog that is created by .ShowDialog(this) is equally simple, with an OK button and a Cancel button:

using System;
using System.Windows.Forms;

public partial class Dialog : Form
{
    public Dialog()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Close();
    }
}

The application starts,

enter image description here

and when the user clicks on the "Show Modal Dialog" button, the button1 event Click is fired, and the dialog is triggered as shown in the first snippet.

enter image description here

When the user clicks on the original form while the dialog is still open, the dialog's title bar flashes.

enter image description here

Is that event accessible via the Windows.Forms API, or by any other means?

In a more complicated application, I would like to close the modal dialog when the user click back on the main form if the dialog's input fields pass validation, and highlight the invalid fields if not.

I'm currently showing the dialog using the .Show() method, and closing the dialog on the deactivate event. But this has two disadvantages

  • When the user clicks on the desktop or another application, the dialog closes.
  • When the user clicks off the dialog, sometimes the main form is hidden behind the window of a different application.

I found a related WPF question, and the answer was a pretty concrete "no".

like image 537
kdbanman Avatar asked Sep 27 '22 07:09

kdbanman


People also ask

What does modal dialog do?

Definition: A modal dialog is a dialog that appears on top of the main content and moves the system into a special mode requiring user interaction. This dialog disables the main content until the user explicitly interacts with the modal dialog.

Is modal dialog is blocking window?

A modal window creates a mode that disables the main window but keeps it visible, with the modal window as a child window in front of it. Users must interact with the modal window before they can return to the parent application.

What is ShowDialog in VB net?

ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.

What is modal dialog box?

A modal dialog box requires the user to close the dialog box before activating another window in the application. However, the user can activate windows in different applications. A modeless dialog box does not require an immediate response from the user.


1 Answers

overriding WndProc is the way to go, as Orion_Eagle suggests. Check out this list of windows messages

protected override void WndProc(ref Message m)
{
    //134 = WM_NCACTIVATE
    if (m.Msg == 134)
    {       
        //Check if other app is activating - if so, we do not close         
        if (m.LParam == IntPtr.Zero)
        {                    
             if (m.WParam != IntPtr.Zero)
             {                     
                 //Other form in our app has focus
             }

        }
     }                     

     base.WndProc(ref m);
}
like image 110
TDull Avatar answered Sep 30 '22 06:09

TDull