Jump to content
softtouch

Close modal form by clicking outside?

Recommended Posts

Lets say I have a modal form , which I display with

 

myform.ShowModal;

How can I close this form when the user click outside the form?

 

The form wont have a caption, and is just showing a listview and no buttons.

The user can click an entry in the listview, and the form close. But the form should also close when the user click outside the form.

 

Share this post


Link to post
26 minutes ago, softtouch said:

How can I close this form when the user click outside the form?

You can call the form's Close() method, or set the form's ModalResult property, in the form's OnDeactivate event.

Share this post


Link to post
10 minutes ago, Remy Lebeau said:

You can call the form's Close() method, or set the form's ModalResult property, in the form's OnDeactivate event.

I know that works for non modal forms, never tried with modal forms.

Just tried with 10.2 and OnDeactivate is not called with a modal form, unless I am doing something wrong.

Share this post


Link to post
2 hours ago, softtouch said:

How can I close this form when the user click outside the form?

You need to capture the mouse in order to receive messages for mouse activity outside the form.

See also: SetCapture

 

You should be able to figure it out with that info.

Share this post


Link to post
2 hours ago, Mark- said:

I know that works for non modal forms, never tried with modal forms.

Just tried with 10.2 and OnDeactivate is not called with a modal form, unless I am doing something wrong.

Sorry, I meant the TApplication(Events).OnDeactivate event, not the form's OnDeactivate event.

 

You can't click on another form in your app since it has been disabled by the modal form.  But, if you click outside of the app then the modal form will lose focus, triggering the TApplication(Events).OnDeactivate event.  You can then close the modal form.

Share this post


Link to post
2 hours ago, Remy Lebeau said:

Sorry, I meant the TApplication(Events).OnDeactivate event, not the form's OnDeactivate event.

 

You can't click on another form in your app since it has been disabled by the modal form.  But, if you click outside of the app then the modal form will lose focus, triggering the TApplication(Events).OnDeactivate event.  You can then close the modal form.

Thanks.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×