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,
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.
When the user clicks on the original form while the dialog is still open, the dialog's title bar flashes.
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".