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
dialog
closes.I found a related WPF question, and the answer was a pretty concrete "no".
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.
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.
ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With