I need a modal form to notify user (e.g. by shaking itself) when user is attempting to access any other form of the application (by mouse clicking or anyhow else). The problem is that I can't detect this attempt. Events /Deactivate,LostFocus,Leave/ just don't work. ADD: the modal form is borderless, so when a user clicks on the parent form (which is disabled when the modal form is open) - NOTHING happens. Form has no border, so it's not flashing. That's why I need some way to notify the user, that he must close the modal form to access the parent one. I decided to shake the modal form to make user pay attention to it. But for this I must catch the event when user tries to access parent form. I don't know how to do this.
-
1And using [`.ShowDialog()`](https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx), that has this functionality, is not an option? – TEK May 30 '16 at 09:46
-
I know how make a form modal (I use ShowDialog). The problem is how to find out programmatically that user is attempting to access parent form when modal form is open. – Alex D. May 30 '16 at 10:05
-
Then you'll need to make that explicit in the question, as it's ambiguous at the moment. – TEK May 30 '16 at 10:07
2 Answers
Use Form.ShowDialog()
instead of Form.Show()
ShowDialog
shows your window as modal, which means you cannot interact with the parent form until it closes.
And you can disable parent form
this.Enabled = false;
MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);

- 1,763
- 3
- 17
- 55
-
I know how to make it modal. But that is not my problem. Please read the topic. – Alex D. May 30 '16 at 10:07
-
1@AlexD. Read your question: _"I need a modal form to notify user (e.g. by shaking itself) when user is attempting to access any other form of the application (by mouse clicking or anyhow else)"_. So that part is actually completely irrelevant? Your question is: "How can I detect when a modal form has not been clicked on?" – TEK May 30 '16 at 10:10
-
I edited the question. English is not my native language, sorry if you could not understand it( – Alex D. May 30 '16 at 10:17
-
http://stackoverflow.com/questions/1576329/how-to-detect-when-an-application-loses-focus – mohsen May 30 '16 at 10:23
-
`Deactivate` event is NOT raised when user tries to remove focus from a modal form. I've tried it already – Alex D. May 30 '16 at 10:27
-
-
I think you must change title of question : why deactivate don't work in modal form – mohsen May 30 '16 at 10:33
You could do something a bit hacky:
Firstly, add a boolean property to your main form (the one that you want to flash, not the modal form) so you can track whether the modal form is shown:
bool inModalForm;
void button1_Click(object sender, EventArgs e)
{
using (var form = new Form2())
{
this.BeginInvoke(new Action(() => inModalForm = true));
form.ShowDialog();
inModalForm = false;
}
}
Set the boolean using BeginInvoke()
because you will get a window message about the main window's position changing after inModalForm
is set, which you don't want.
Then override WndProc()
in your main form as follows:
protected override void WndProc(ref Message m)
{
const int WM_WINDOWPOSCHANGING = 0x46;
if (inModalForm && (m.Msg == WM_WINDOWPOSCHANGING))
{
Debug.WriteLine("Someone's trying to activate the window.");
}
base.WndProc(ref m);
}
Make your window flash where I put the Debug.WriteLine(). I should think you will need to use this.BeginInvoke()
to call your flash method; you don't want to be doing it from inside WndProc()
!
This isn't perfect because I think you can get the occasional spurious message (for example if neither form has the focus and you then click on the modal form), but perhaps it will do.

- 104,400
- 10
- 158
- 276