Close a form when clicked in the form background. Form is opened using form.ShowDialog().


New member
Jul 10, 2021
Programming Experience
I'm working on word add-in, I have a button which when clicked opens a windows form using form.ShowDialog(). I want to close the form whenever user clicks outside the form area, I want to close because form.ShowDialog() doesn't allow me to minimise it and I'm saving the instance of the form before closing so that when user clicks the button again then the same instance of the form is opened(client requirement). I have word opened in the background of the form. I tried using the deactivate event, it works as expected only if I click on the taskbar. But it doesn't work if I click on the form background that is on the word document open in form background.

So, does anybody know any such event or work-around to fix this?

NOTE: I have tried using the following events:
Deactivate, Leave, MouseLeave, LostFocus. But none of these helped. I'm not using form.Show() because I want the execution to stop when the form is opened which form.ShowDialog() allows me to do.


Staff member
Apr 6, 2019
Chesapeake, VA
Programming Experience
The major issue here is that you are trying to do be web like behaviour in a Windows program. In the Windows UI guidelines, dialogs are supposed to stay open until dismissed by explicitly clicking on a button. This applies for modal and modeless dialogs. You are trying to implement a Web 2.0 behaviour (which I personally hate) where clicking outside the dialog brought up in a shadow box closes the shadow box. If you working with a Windows app please, follow the Windows UI guidelines. It is inventing your own UI rules like this that gives Windows a bad reputation for having inconsistent UI, while Macs get all the praise for a smooth consistent UI.
Top Bottom