469,917 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Preventing an MDI child from being closed

I would like to prevent the user from closing MDI child windows. However, I
would like them to be able to minimize and maximize them.

Setting the Control Box to false removes the minimize and maximize too.

An obvious thing to do is to handling the CLosing event and cancel it.
Sadly, if you do this the main Form refuses to shut down...

A solution to this is to intercept the Closing event on the main form and
use it to set a boolean that the children can check to see if they CAN close
down.

But this is raised *after* the childrens events.

Hmmm.

Any one know how I can sort this out?

Iain
Nov 15 '05 #1
5 4053
"Iain" <id********@dircon.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I would like to prevent the user from closing MDI child windows. However, I would like them to be able to minimize and maximize them.

Setting the Control Box to false removes the minimize and maximize too.

An obvious thing to do is to handling the CLosing event and cancel it.
Sadly, if you do this the main Form refuses to shut down...

A solution to this is to intercept the Closing event on the main form and
use it to set a boolean that the children can check to see if they CAN close down.

But this is raised *after* the childrens events.


As a user I would be offended by you inhibiting me from closing the MDI
child. If you don't want me to close a child, the close option should be
disabled. It would be wrong to offer the close option, accept the command
and than fail to comply.

MDI has practically been deprecated for some time now, Microsoft does not
encourage to use it.

In an MDI application, all MDI Windows should contain documents of the same
type. If you are presenting different types in different MDI windows, MDI
does not apply and it should not be used.

If you are showing singleton documents/objects of different types in MDI
child windows and you were to ignore my valuable advise to not abuse the MDI
model, you could make the result less annoying on the user by accepting the
close command and hiding the child instead of closing it. The MDI window's
View menu could them offer a list of views that can be checkmarked in order
to make them visible or hide them. Close on a child would be the shortcut
for going into the menu and hiding it.

Martin.
Nov 15 '05 #2
Thanks for your comments, Martin.

The MDI format suits the application I have built which has a number of
'management consoles'. I don't much care if MS do not approve! (I could
argue if pressed that they are each simply a view of the underlying
database).

There appears no way of disabling only the close box. Certainly not without
disabling the minimize and maximize.

Even if I picked a hide rather than close option, I suspect I would still be
unable to close the application. After all to hide the window I would have
to cancel the close which seems to have the effect of cancelling the main
window close.

Any other thoughts?

Iain
As a user I would be offended by you inhibiting me from closing the MDI
child. If you don't want me to close a child, the close option should be
disabled. It would be wrong to offer the close option, accept the command
and than fail to comply.

MDI has practically been deprecated for some time now, Microsoft does not
encourage to use it.

In an MDI application, all MDI Windows should contain documents of the same type. If you are presenting different types in different MDI windows, MDI
does not apply and it should not be used.

If you are showing singleton documents/objects of different types in MDI
child windows and you were to ignore my valuable advise to not abuse the MDI model, you could make the result less annoying on the user by accepting the close command and hiding the child instead of closing it. The MDI window's
View menu could them offer a list of views that can be checkmarked in order to make them visible or hide them. Close on a child would be the shortcut
for going into the menu and hiding it.

Martin.

Nov 15 '05 #3
You could always hide all the controls since you can't hide the X only. Then put buttons on your form to make it minimize and maximize.
Nov 15 '05 #4
Yeah.

I was kind of hoping for something a little less stressful.

What I've decided to do is to remove the control box completely from 2 of my
forms (which really really need to be there all the time) and have the rest
available from the File Open menu (exception that Open when a window is
present simply restores it).

Bit naughty, but it works!

Thanks for the suggestion.

Iain
"Eric" <an*******@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com...
You could always hide all the controls since you can't hide the X only.

Then put buttons on your form to make it minimize and maximize.
Nov 15 '05 #5
> The MDI format suits the application I have built which has a number of
'management consoles'. I don't much care if MS do not approve! (I could
argue if pressed that they are each simply a view of the underlying
database). There appears no way of disabling only the close box. Certainly not without disabling the minimize and maximize.
Well... Maybe no .NET way. But since you are such a bad boy, not caring
about anything, you might as well do it through Win32. Here is the MFC way
to disable the close command (any menu options will automatically follow).

void CMainFrame::DisableClose()
{
CMenu* pMenu = GetSystemMenu(false);
pMenu->EnableMenuItem(SC_CLOSE, MF_DISABLED | MF_GRAYED);
}

void CMainFrame::EnableClose()
{
CMenu* pMenu = GetSystemMenu(false);
pMenu->EnableMenuItem(SC_CLOSE, MF_ENABLED);
}

Here a CMenu is used but this doesn't do much more than passing on the call
to a Win32 API function. It won't be hard to change the code to use WIn32
only. I won't help you doing it, I feel guilty enough as it is by providing
this information.
Even if I picked a hide rather than close option, I suspect I would still be unable to close the application. After all to hide the window I would have to cancel the close which seems to have the effect of cancelling the main
window close.

Any other thoughts?


No, still the same thought. You are abusing MDI and you are being served
right by the trouble you are getting yourself into doing so. Design
guidelines are not only there to protect the user, most of the time they
also protect the developer.

Really, it's for your own good :-).

Martin.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jon | last post: by
2 posts views Thread by Jon | last post: by
6 posts views Thread by Edwinah63 | last post: by
reply views Thread by Rotsey | last post: by
6 posts views Thread by SAL | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.