I have an open modal dialog (Windows Forms). I want, that the dialog is closed when clicking outside the dialog (on the parent form). How can I do that?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Use .Show() instead of .ShowDialog().
Handle Deactivate event in child form.
This would close the child form when user clicks outside the child.
If you are using .ShowDialog() for doing something after the child closed, use .show() and override the 'formclosing' event of child in the parent. So it would hit when the child form closes and do your stuff there.
Hope this helps.
hi I think u can use the mouse down event or keydown event in the event find the location fo the mouse if that location is in minus values that means u have selected the outer area of the windows and then close that windows hope this help.
You should change it to a non-modal dialog (open it with
Show(..)
) and then use theDeactivate
event and close it.That cannot work, you must use Show() to get the Deactivate event to fire.
A dialog disables all of the other windows to make itself modal. So there's nothing that can be clicked on outside of the dialog window with the parent maximized. Accordingly, the Deactivate event isn't going to fire. When you use the Show(owner) method instead, this side-effect of ShowDialog() no longer gets in the way and Deactivate is fine. Use the FormClosing/Closed event to do what you would do after a ShowDialog() call.
The way this is normally done, with a context menu strip or menu dropdown for example, is by capturing the mouse, Control.Capture property. So that you can detect mouse events even outside of the window. But that can't work reliably with forms, the controls inside the form will use the Capture property for their own use. So stick with Show and Deactivate.
There are reasons (e.g. framework workarounds etc.) which force one to use a modal dialog. You can emulate the
Deactivate
event by overriding the form methodWndProc
like this:The
WM_NCACTIVATE
message is send to tell the form to indicate whether it is active (WParam == 1
) or inactive (WParam == 0
). If you click somewhere on the parent form your modal dialog begins to "blink with activity" telling you, that it is more important right now. This is achieved by sending the previously mentioned Messages a few times (deactivate -> activate -> deactivate -> ... -> activate).But be careful, if the
handleDeactivate()
method is not terminating your form, it will be called every time the deactivate messages come. You have to handle that yourself.Links:
MSDN Forum
MSDN Documentation